SqlConnectionStringBuilder Constructor (String)
Collapse the table of content
Expand the table of content

SqlConnectionStringBuilder Constructor (String)

 

Initializes a new instance of the SqlConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information.

Namespace:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

Public Sub New (
	connectionString As String
)

Parameters

connectionString
Type: System.String

The basis for the object's internal connection information. Parsed into name/value pairs. Invalid key names raise KeyNotFoundException.

Exception Condition
KeyNotFoundException

Invalid key name within the connection string.

FormatException

Invalid value within the connection string (specifically, when a Boolean or numeric value was expected but not supplied).

ArgumentException

The supplied connectionString is not valid.

The SqlConnectionStringBuilder class provides a fixed internal collection of key/value pairs. Even if you supply only a small subset of the possible connection string values in the constructor, the object always provides default values for each key/value pair. When the ConnectionString property of the object is retrieved, the string contains only key/value pairs in which the value is not the default value for the item.

The following example supplies a simple SQL Server connection string in the SqlConnectionStringBuilder object's constructor, and then iterates through all the key/value pairs within the object. Note that the collection provides default values for each item. Also note that the SqlConnectionStringBuilder class converts synonyms for the well-known keys so that they are consistent with the well-known names.

System_CAPS_noteNote

This example includes a password to demonstrate how SqlConnectionStringBuilder works with connection strings. In your applications, we recommend that you use Windows Authentication. If you must use a password, do not include a hard-coded password in your application.

Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Try
            Dim connectString As String = _
                "Server=(local);Database=AdventureWorks;UID=ab;Pwd=a!Pass@@"
            Console.WriteLine("Original: " & connectString)
            Dim builder As New SqlConnectionStringBuilder(connectString)
            Console.WriteLine("Modified: " & builder.ConnectionString)
            For Each key As String In builder.Keys
                Console.WriteLine(key & "=" & builder.Item(key).ToString)
            Next
            Console.WriteLine("Press any key to finish.")
            Console.ReadLine()

        Catch ex As System.Collections.Generic.KeyNotFoundException
            Console.WriteLine("KeyNotFoundException: " & ex.Message)
        Catch ex As System.FormatException
            Console.WriteLine("Format exception: " & ex.Message)
        End Try
    End Sub
End Module 

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft