DbConnectionStringBuilder.Add(String, Object) 方法
定义
将带有指定键和值的条目添加到 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)
参数
- keyword
- String
要添加到 DbConnectionStringBuilder 的键。The key to add to the DbConnectionStringBuilder.
- value
- Object
指定键的值。The value for the specified key.
例外
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.
示例
下面的示例创建一个新的 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.
备注
该示例包括一个密码以演示 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
注解
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"] = myValue
。For example: myCollection["myNonexistentKey"] = myValue
.
Add通过 Nothing
在 Visual Basic) 键中传递 null (来调用方法会引发 ArgumentNullException 。Calling the Add method by passing a null (Nothing
in Visual Basic) key throws an ArgumentNullException. 但是, Add 通过传递 null 值调用方法会删除键/值对。However, calling the Add method by passing a null value removes the key/value pair.