Configuration.ConnectionStrings Property

 

Gets a ConnectionStringsSection configuration-section object that applies to this Configuration object.

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

public ConnectionStringsSection ConnectionStrings { get; }

Property Value

Type: System.Configuration.ConnectionStringsSection

A ConnectionStringsSection configuration-section object representing the connectionStrings configuration section that applies to this Configuration object.

Use the ConnectionStrings property to access and change the connectionStrings configuration section defined in the open configuration file.

The following code example demonstrates how to use the ConnectionStrings property.

ConnectionStringsSection
    conStrSection =
    config.ConnectionStrings as ConnectionStringsSection;
Console.WriteLine("Section name: {0}",
    conStrSection.SectionInformation.SectionName);

try
{
    if (conStrSection.ConnectionStrings.Count != 0)
    {
        Console.WriteLine();
        Console.WriteLine("Using ConnectionStrings property.");
        Console.WriteLine("Connection strings:");

        // Get the collection elements.
        foreach (ConnectionStringSettings connection in
          conStrSection.ConnectionStrings)
        {
            string name = connection.Name;
            string provider = connection.ProviderName;
            string connectionString = connection.ConnectionString;

            Console.WriteLine("Name:               {0}",
              name);
            Console.WriteLine("Connection string:  {0}",
              connectionString);
            Console.WriteLine("Provider:            {0}",
               provider);
        }
    }
}
catch (ConfigurationErrorsException e)
{
    Console.WriteLine("Using ConnectionStrings property: {0}",
        e.ToString());
}

.NET Framework
Available since 2.0
Return to top
Show: