Export (0) Print
Expand All

ConfigurationManager.ConnectionStrings Property

Gets the ConnectionStringsSection data for the current application's default configuration.

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

'Declaration
Public Shared ReadOnly Property ConnectionStrings As ConnectionStringSettingsCollection

Property Value

Type: System.Configuration.ConnectionStringSettingsCollection
Returns a ConnectionStringSettingsCollection object that contains the contents of the ConnectionStringsSection object for the current application's default configuration.

A ConnectionStringsSection object contains the contents of the configuration file's connectionStrings section.

The following example shows how to use a connection string to read data from a database.

Imports System.Configuration
Imports System.Data.SqlClient
Module Module1

    Sub Main()
        ReadProducts()
    End Sub

    Sub ReadProducts()
        Dim connectionString = ConfigurationManager.ConnectionStrings("WingtipToys").ConnectionString
        Dim queryString = "SELECT Id, ProductName FROM dbo.Products;"
        Using connection As New SqlConnection(connectionString)
            Dim command = New SqlCommand(queryString, connection)
            connection.Open()

            Using reader As SqlDataReader = command.ExecuteReader()
                While reader.Read()
                    Console.WriteLine(String.Format("{0}, {1}", reader(0), reader(1)))
                End While
            End Using
        End Using
    End Sub

End Module

The previous example assumes your project has an App.config as shown below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <connectionStrings>
      <add name="WingtipToys" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False" />
    </connectionStrings>
</configuration>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft