Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ConnectionStringsSection Class

Provides programmatic access to the connection strings configuration-file section.

Namespace:  System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)
Visual Basic (Declaration)
Public NotInheritable Class ConnectionStringsSection _
    Inherits ConfigurationSection
Visual Basic (Usage)
Dim instance As ConnectionStringsSection
C#
public sealed class ConnectionStringsSection : ConfigurationSection
Visual C++
public ref class ConnectionStringsSection sealed : public ConfigurationSection
JScript
public final class ConnectionStringsSection extends ConfigurationSection

The ConnectionStringsSection class allows you to programmatically modify the connectionStrings section of the configuration file. The connection strings are provided as a ConnectionStringSettingsCollection of ConnectionStringSettings add configuration elements.

TopicLocation
How to: Access SQL Server Using Predetermined CredentialsBuilding ASP .NET Web Applications
How to: Access SQL Server Using Predetermined CredentialsBuilding ASP .NET Web Applications
How to: Access SQL Server Using Predetermined CredentialsBuilding ASP .NET Web Applications in Visual Studio
How to: Access SQL Server Using Predetermined CredentialsBuilding ASP .NET Web Applications in Visual Studio
How to: Read Connection Strings from the Web.config FileConfiguring ASP .NET Web Applications
How to: Read Connection Strings from the Web.config FileConfiguring ASP .NET Web Applications
How to: Read Connection Strings from the Web.config FileBuilding ASP .NET Web Applications in Visual Studio
How to: Read Connection Strings from the Web.config FileBuilding ASP .NET Web Applications in Visual Studio
How to: Secure Connection Strings When Using Data Source ControlsBuilding ASP .NET Web Applications
How to: Secure Connection Strings When Using Data Source ControlsBuilding ASP .NET Web Applications
How To: Secure Connection Strings when Using Data Source ControlsBuilding ASP .NET Web Applications in Visual Studio
How To: Secure Connection Strings when Using Data Source ControlsBuilding ASP .NET Web Applications in Visual Studio
How To: Secure Connection Strings when Using Data Source Controls (Visual Studio)Building ASP .NET Web Applications in Visual Studio

The following example shows how to use the ConnectionStringsSection.

Visual Basic
' Create a connectionn string element and add it to
' the connection strings section.
Shared Sub New() 

    ' Get the application configuration file.
    Dim config _
    As System.Configuration.Configuration = _
    ConfigurationManager.OpenExeConfiguration( _
    ConfigurationUserLevel.None)

    ' Get the current connection strings count.
    Dim connStrCnt As Integer = _
    ConfigurationManager.ConnectionStrings.Count

    ' Create the connection string name. 
    Dim csName As String = "ConnStr" + connStrCnt.ToString()

    ' Create a connection string element and
    ' save it to the configuration file.
    ' Create a connection string element.
    Dim csSettings _
    As New ConnectionStringSettings( _
    csName, _
    "LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" + _
    "Initial Catalog=aspnetdb", "System.Data.SqlClient")

    ' Get the connection strings section.
    Dim csSection _
    As ConnectionStringsSection = _
    config.ConnectionStrings

    ' Add the new element.
    csSection.ConnectionStrings.Add(csSettings)

    ' Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified)

End Sub 'New


C#
// Create a connectionn string element and add it to
// the connection strings section.
static ConnectionStrings()
{
    // Get the application configuration file.
    System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);

    // Get the current connection strings count.
    int connStrCnt = 
        ConfigurationManager.ConnectionStrings.Count;

    // Create the connection string name. 
    string csName = 
        "ConnStr" + connStrCnt.ToString();

    // Create a connection string element and
    // save it to the configuration file.

    // Create a connection string element.
    ConnectionStringSettings csSettings =
            new ConnectionStringSettings(csName,
            "LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
            "Initial Catalog=aspnetdb", "System.Data.SqlClient");

    // Get the connection strings section.
    ConnectionStringsSection csSection =
        config.ConnectionStrings;

    // Add the new element.
    csSection.ConnectionStrings.Add(csSettings);

    // Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified);

}

The following excerpt shows the configuration used by the previous example.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
System..::.Object
  System.Configuration..::.ConfigurationElement
    System.Configuration..::.ConfigurationSection
      System.Configuration..::.ConnectionStringsSection
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker