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 ReadOnly Property ConnectionStrings As ConnectionStringsSection

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.

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

Try
    If conStrSection.ConnectionStrings.Count <> 0 Then
        Console.WriteLine()
        Console.WriteLine("Using ConnectionStrings property.")
        Console.WriteLine("Connection strings:")

        ' Get the collection elements.
        For Each connection As ConnectionStringSettings In conStrSection.ConnectionStrings
            Dim name As String = connection.Name
            Dim provider As String = connection.ProviderName
            Dim connectionString As String = connection.ConnectionString

            Console.WriteLine("Name:               {0}", name)
            Console.WriteLine("Connection string:  {0}", connectionString)
            Console.WriteLine("Provider:            {0}", provider)
        Next connection
    End If
Catch e As ConfigurationErrorsException
    Console.WriteLine("Using ConnectionStrings property: {0}", e.ToString())
End Try

.NET Framework
Available since 2.0
Return to top
Show: