DbConnectionStringBuilder.Add(String, Object) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
The key to add to the DbConnectionStringBuilder.
- value
- Object
The value for the specified key.
Exceptions
keyword
is a null reference (Nothing
in Visual Basic).
Examples
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.
Note
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();
}
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
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.
Applies to
See also
Feedback
https://aka.ms/ContentUserFeedback.
Binnenkort: Gedurende 2024 worden GitHub Issues uitgefaseerd als het feedbackmechanisme voor inhoud. Dit wordt vervangen door een nieuw feedbacksysteem. Ga voor meer informatie naar:Feedback verzenden en bekijken voor