Export (0) Print
Expand All

DbConnectionStringBuilder.Add Method

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

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

public void Add (
	string keyword,
	Object value
)
public void Add (
	String keyword, 
	Object value
)
public function Add (
	keyword : String, 
	value : Object
)
Not applicable.

Parameters

keyword

The key to add to the DbConnectionStringBuilder.

value

The value for the specified key.

Exception typeCondition

ArgumentNullException

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

NotSupportedException

The DbConnectionStringBuilder is read-only.

-or-

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.

NoteNote:

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.

static void Main()
{
    try
    {
        DbConnectionStringBuilder builder =
            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(null, "Some Value");

        Console.WriteLine(builder.ConnectionString);
    }
    catch (ArgumentNullException)
    {
        Console.WriteLine("Null key values are not allowed.");
    }

    Console.WriteLine("Press Enter to continue.");
    Console.ReadLine();
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft