Storing and Retrieving Connection StringsĀ 

We recommend that you not embed connection strings in your code. If the location of the server should ever change, your application will need to be recompiled. In addition, unencrypted connection strings compiled into an application's source code can be viewed using the MSIL Disassembler (ildasm.exe). To avoid storing connection strings in your code, you can store them in the web.config file for an ASP.NET application and in the app.config file for a Windows application. Although configuration files are mainly used in ASP.NET applications where they are protected from being browsed to, you can also use them in a Windows application. The syntax and format of the app.config and web.config files is identical for both ASP.NET and Windows applications.

Storing Connection Strings in Configuration Files

The connection string can be stored in the configuration file in the <connectionStrings> element. Connection strings are stored as key/value pairs, where the name key can be used to look up the value stored in the connectionString attribute at run time. For more information on the connectionStrings element, see connectionStrings Element (ASP.NET Settings Schema).

The following example of the connectionStrings element of a configuration file shows a connection string named DatabaseConnection that references a connection string that connects to the Northwind database on a local instance of SQL Server.

<connectionStrings>
    <add name="DatabaseConnection" 
        connectionString="Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local);"
         providerName="System.Data.SqlClient" />
</connectionStrings>

You can work with configuration files programmatically by using the Configuration class. The methods of the WebConfigurationManager object allow you to obtain a configuration section, each of which has its own object type. For more information, see Using the Configuration Classes.

Retrieving Connection Strings from Configuration Files

The System.Configuration namespace provides classes for working with configuration information stored in configuration files. The ConnectionStringSettings class has two properties, which map to the names shown in the sample <connectionStrings> section shown above.

  • Name
    The name of the connection string in the <connectionStrings> section.
Security noteSecurity Note

You can use protected configuration to optionally encrypt connection strings stored in configuration files. See Encrypting Configuration Information Using Protected Configuration, Encrypting and Decrypting Configuration Sections, and Walkthrough: Encrypting Configuration Information Using Protected Configuration.

Example

The following example retrieves the connection string from the configuration file by passing the name of the connection string to the ConfigurationManager, which returns a ConnectionStringSettings object. The ConnectionString property is used to display the value.

Option Explicit On
Option Strict On

Imports System.Configuration

Module Module1
    Sub Main()
        Dim settings As ConnectionStringSettings
        settings = _
           ConfigurationManager.ConnectionStrings("DatabaseConnection")
        If Not settings Is Nothing Then
            Console.WriteLine(settings.ConnectionString)
        End If
    End Sub
End Module
using System;
using System.Configuration;

class Program
{
    static void Main()
    {
        ConnectionStringSettings settings;
        settings = 
            ConfigurationManager.ConnectionStrings["DatabaseConnection"];
        if (settings != null)
        {
            Console.WriteLine(settings.ConnectionString);
        }
    }
}

Additional code examples demonstrating how to retrieve connection strings from configuration files can be found in Building Connection Strings and How to: Read Connection Strings from the Web.config File.

See Also

Concepts

Securing Connection Strings
Using the Configuration Classes

Other Resources

Working with Connection Strings