Anyone know how I can aquire my database connection string?

My professor taught me a way like so:

[IMG][/IMG]

[IMG][/IMG]

[IMG][/IMG]

[IMG][/IMG]


However after click the new connection button the window that is supposed to open doesn't open. I don't know what's wrong. Is there any other way to obtain the connection string?

Dani AI

Generated

Short summary: reported the Data Source Configuration wizard failing to open in Visual C# 2008 while targeting SQL Server 2008. already suggested the TableAdapter/wizard route and posted helper methods for building strings. Below are reliable alternatives and troubleshooting tips that avoid the wizard and give a working connection string immediately.

Programmatic builder (safe, strongly typed)

using System.Data.SqlClient;

var b = new SqlConnectionStringBuilder {
    DataSource = @".\SQLEXPRESS",    // server\instance or "." for local
    InitialCatalog = "MyDatabase",
    IntegratedSecurity = true        // or set UserID and Password
};
string connectionString = b.ConnectionString;

Store and read from configuration (desktop apps)

<!-- app.config -->
<connectionStrings>
  <add name="DefaultDb" connectionString="..." providerName="System.Data.SqlClient"/>
</connectionStrings>
using System.Configuration;
string cs = ConfigurationManager.ConnectionStrings["DefaultDb"].ConnectionString;

Practical steps and troubleshooting

  • Confirm the SQL Server service and instance name (SQL Server Configuration Manager or Services). Common Express names: .\SQLEXPRESS or MachineName\SQLEXPRESS.
  • If the wizard is missing (reported for some Express installs), use Server Explorer or SQL Server Management Studio to test connections, then capture server/credentials and build the string as above.
  • If Visual Studio UI is the problem, repairing the VS/SQL Server tools install or installing the SQL Server client tools typically restores the data wizards.
  • Security note: avoid hardcoding plain-text passwords in source. Prefer Windows authentication or protect/encrypt connection strings in config.

This complements ’s approach by showing a low-friction, code-first workflow and recovery steps for when the VS wizard will not appear.

Recommended Answers

All 6 Replies

Please, I know someone here has to know something about this subject.

Only 30 characters between me and my finished program! I have everything else already made, I just need that darn connection string. :P

Please help me guys.

Drop a table adapter on the form and it will pop the wizard up to create a connection string. Once you have created the connection string and saved it, you can access it in Properties.Settings.Default.ConnectionStringName

OR

You could manually make the connection string, see:
www.connectionstrings.com

Where do I find this table adaptar on the toolbox?

Edit: After reading your post: Are you sure your not doing the same thing I'm doing in my pictures? Because when I click the "New Connection" button in the data source configuration button nothing opens up and I'm tossed back to the form.

I know for a fact that a small window should open and allow me to select what type of database i have, etc.

Any help?

Sorry, I missed the part about the connection wizard not being invoked. I have read about that happening with VS.NET Express installations but i'm not sure as to the cause or solution. You didn't specify what type/verson of database you were trying to connect to so here are some functions to generate the connection string you need. If these do not suit your needs then post what DB you're using, and you are trying to get the Connection String, right? Not trying to figure out why the connection dialog won't open? Those are two different issues....

public static string BuildSqlNativeConnStr(string server, string database)
    {
      return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
    }
    public static string BuildSqlNativeConnStr(string server, string database, string username, string password)
    {
      return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;User Id={2};Password={3};", 
        server, 
        database,
        username,
        password);
    }
    public static string BuildExcelConnectionString(string Filename, bool FirstRowContainsHeaders)
    {
      return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=\"Excel 8.0;HDR={1};\"",
                          Filename.Replace("'", "''"),
                          FirstRowContainsHeaders ? "Yes" : "No");
    }
    public static string BuildExcel2007ConnectionString(string Filename, bool FirstRowContainsHeaders)
    {
      return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR={1}\";",
                            Filename.Replace("'", "''"),
                            FirstRowContainsHeaders ? "Yes" : "No");
    }
    public static string BuildAccessConnectionString(string Filename, string Username, string Password, string DatabasePassword)
    {
      return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';User Id={1};Password={2};Jet OLEDB:Database Password={3};",
                                   Filename.Replace("'", "''"),
                                   Username,
                                   Password,
                                   DatabasePassword);
    }
    public static string BuildAccess2007ConnectionString(string Filename, string DatabasePassword)
    {
      return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Persist Security Info=False;Jet OLEDB:Database Password={1};",
                                 Filename.Replace("'", "''"),
                                 DatabasePassword);
    }
    public static string BuildODBCConnectionString(string DSN)
    {
      return string.Format("DSN={0}", DSN);
    }

Thank you for trying to help me out. :)

I am using SQL Server 2008 and Visual C# 2008 as my IDE.

I really don't understand what the code above does though. Any extra help? Pretty please. xD

It generates the connection string for you.... Do you have a local copy of SQL Server running? If so you should be able to hover your mouse over the icon in the lower right of your machine and it should look like "ComputerName" or "ComputerName\InstanceName"

So take that text and plug it in here:
Data Source=MyPCName\MyInstanceName; Initial Catalog= MyDatabaseName; Integrated Security=True;

Replace "MyPCName\MyInstanceName" with what you see when you hover your mouse, replace "MyDatabaseName" with the DB name you want to connect to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.