DbConnectionStringBuilder.Add(String, Object) Method

Definition

将带有指定键和值的条目添加到 DbConnectionStringBuilder 中。Adds an entry with the specified key and value into the DbConnectionStringBuilder.

public:
 void Add(System::String ^ keyword, System::Object ^ value);
public void Add (string keyword, object value);
member this.Add : string * obj -> unit
Public Sub Add (keyword As String, value As Object)

Parameters

keyword
String

要添加到 DbConnectionStringBuilder 的键。The key to add to the DbConnectionStringBuilder.

value
Object

指定的键的值。The value for the specified key.

Exceptions

keyword 为空引用(在 Visual Basic 中为 Nothing)。keyword is a null reference (Nothing in Visual Basic).

DbConnectionStringBuilder 为只读。The DbConnectionStringBuilder is read-only.

- 或 --or-

DbConnectionStringBuilder 具有固定的大小。The DbConnectionStringBuilder has a fixed size.

Examples

下面的示例创建一个新的 DbConnectionStringBuilder 并添加项。The following example creates a new DbConnectionStringBuilder and adds items. 该代码还演示了如何使用 Add 方法覆盖现有项,并包含一个将引发 ArgumentNullException的注释块。The code also demonstrates overwriting an existing item using the Add method, and includes a commented block that would throw an ArgumentNullException.

Note

该示例包括一个密码以演示 DbConnectionStringBuilder 如何使用连接字符串。This example includes a password to demonstrate how DbConnectionStringBuilder works with connection strings. 在您的应用程序中,建议使用 Windows 身份验证。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();
}
Sub Main()
    Try
        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")

        Console.WriteLine(builder.ConnectionString)

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

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

Remarks

Item[] 属性还可用于通过设置字典中不存在的键的值来添加新元素。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. 例如:myCollection["myNonexistentKey"] = myValueFor example: myCollection["myNonexistentKey"] = myValue.

通过传递 null (在 Visual Basic 中Nothing)键调用 Add 方法将引发 ArgumentNullExceptionCalling the Add method by passing a null (Nothing in Visual Basic) key throws an ArgumentNullException. 但是,通过传递 null 值调用 Add 方法将删除键/值对。However, calling the Add method by passing a null value removes the key/value pair.

Applies to

See also