DbConnectionStringBuilder.Add Method
Assembly: System.Data (in system.data.dll)
'Declaration Public Sub Add ( _ keyword As String, _ value As Object _ ) 'Usage Dim instance As DbConnectionStringBuilder Dim keyword As String Dim value As Object instance.Add(keyword, value)
public void Add ( String keyword, Object value )
public function Add ( keyword : String, value : Object )
Parameters
- keyword
The key to add to the DbConnectionStringBuilder.
- value
The value for the specified key.
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.
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. |
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
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note