DbConnectionStringBuilder.Add Method (String, Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Adds an entry with the specified key and value into the DbConnectionStringBuilder.

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

Public Sub Add (
	keyword As String,
	value As Object


Type: System.String

The key to add to the DbConnectionStringBuilder.

Type: System.Object

The value for the specified key.

Exception Condition

keyword is a null reference (Nothing in Visual Basic).


The DbConnectionStringBuilder is read-only.


The DbConnectionStringBuilder has a fixed size.

The Item property can also be used to add new elements by setting the value of a key that does not exist in the dictionary. For example: myCollection["myNonexistentKey"] = myValue.

Calling the Add method by passing a null (Nothing in Visual Basic) key throws an ArgumentNullException. However, calling the Add method by passing a null value removes the key/value pair.

The following example creates a new DbConnectionStringBuilder and adds items. The code also demonstrates overwriting an existing item using the Add method, and includes a commented block that would throw an ArgumentNullException.


This example includes a password to demonstrate how DbConnectionStringBuilder 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.

Sub Main()
        Dim builder As New DbConnectionStringBuilder
        builder.Add("Data Source", "ServerName")
        builder.Add("Initial Catalog", "TheDatabase")
        builder.Add("User ID", "UserName")
        builder.Add("Password", "*******")
        builder.Add("Command Logging", False)

        ' Overwrite the existing "User ID" value.
        builder.Add("User ID", "NewUserName")

        ' The following code would trigger 
        ' an ArgumentNullException.
        ' builder.Add(Nothing, "Some Value")


    Catch ex As ArgumentNullException
        Console.WriteLine("Null key values are not allowed.")
    End Try

    Console.WriteLine("Press Enter to continue.")
End Sub

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top