Skip Navigation LinksHome > SSW Standards > Rules > SSW Rules to Better Connection Strings

There was an error displaying the testimonials. Please report this error to SSW and include the following text:
- A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)


Do you agree with them all? Are we missing some? Let us know what you think.

  1. Do you know how to make connection strings among different environment?

    Different data sources provide different connection strings, for example, the connection string for Access is totally different from SQL Server's, even the same product but different versions have special requirement, so how do we make it easy?

    The answer is the very useful website -- , which provides abundant connection strings ranging from different database products to excel files, actually, you can find most of the connection strings of popular products with detail usage.

  2. Do you avoid putting connection strings in Business module?

    We strongly recommend that putting all connection strings together and saving them to one file, it is convenient for management and deployment.

    Saving connection strings in Business module.
    Bad example - Putting connection strings in Business module.
  3. Do you avoid using non strongly typed connection strings?

    Using non strongly typed connection strings means that you have to hard code at some point in your code. Once you change the name of your connection strings, you have to change the code that references them too.

    Visual Studio 2005 and higher provide a convenient tool, called Application Settings, that allows you to manage all of your connection strings from only one location. You can use its wizard to compose connection strings quickly and correctly. Also, it provides a management class to read and write all of your connection strings.

    string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["WinFormTester.Properties.Settings.ConnectString"].ToString();
    SqlConnection conn = new SqlConnection(ConnString);
    SqlCommand cmd = new SqlCommand(strSql, conn);
    Bad Example: Using non strongly typed connection strings, the highlighted text is hard code actually.
    Using Application Settings to configure connection strings.
    Good example - Using Application Settings to configure connection strings.
  4. Do you add the Application Name in the SQL Server connection string?

    When you make connection strings for SQL Server, it is better to add the application name to your connection strings, see the explicit explanation..

  5. Do you know the best place to place the connection string

    The best place to put the connection string is in the Web.Config file.That makes the code simple and easy to read. Look into the following code:

    string cnnString = "data source=(local); integrated security=SSPI; persist security info=False; pooling=False; initial catalog=Northwind2";
    Bad Example

    and observe the following code which is simple and easy to read:
    string cnnString = LinqToNorthwind.Properties.Settings.Default.NorthwindEFConnectionString;
    Good Example
    private void Form1_Load(object sender, System.EventArgs e)
    //string connString = "data source=(local); integrated security=SSPI; persist security info=False; pooling=False; initial catalog=Northwind2";
    string cnnString = LinqToNorthwind.Properties.Settings.Default.NorthwindEFConnectionString;
    cboCity.Items.Add("Sao Paulo");
    db = new NorthwindDataContext(cnnString);
    cboCity.SelectedIndex = 0;
    Good Example


Adam Cogan
Eric Phan