OleDbConnectionStringBuilder.Clear 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.
Clears the contents of the OleDbConnectionStringBuilder instance.
public:
override void Clear();
public override void Clear ();
override this.Clear : unit -> unit
Public Overrides Sub Clear ()
Examples
The following example demonstrates the effect of calling the Clear method. This example populates the OleDbConnectionStringBuilder with some key/value pairs, and then calls the Clear method and shows the results.
Note
This example includes a password to demonstrate how OleDbConnectionStringBuilder 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.
using System.Data.OleDb;
class Program
{
static void Main()
{
OleDbConnectionStringBuilder builder =
new OleDbConnectionStringBuilder();
builder.ConnectionString = @"Data Source=c:\Sample.mdb";
// Call the Add method to explicitly add key/value
// pairs to the internal collection.
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
builder.Add("Jet OLEDB:Database Password", "MyPassword!");
builder.Add("Jet OLEDB:System Database", @"C:\Workgroup.mdb");
// set up row-level locking.
builder.Add("Jet OLEDB:Database Locking Mode", 1);
// Dump the contents of the "filled-in"
// OleDbConnectionStringBuilder
// to the console window.
DumpBuilderContents(builder);
// Clear current values and reset known keys to their
// default values.
builder.Clear();
// Dump the contents of the newly emptied
// OleDbConnectionStringBuilder
// to the console window.
DumpBuilderContents(builder);
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
private static void DumpBuilderContents(OleDbConnectionStringBuilder builder)
{
Console.WriteLine("=================");
Console.WriteLine("builder.ConnectionString = " + builder.ConnectionString);
foreach (string key in builder.Keys)
{
Console.WriteLine(key + "=" + builder[key].ToString());
}
}
}
Imports System.Data.OleDb
Module Module1
Sub Main()
Dim builder As New OleDbConnectionStringBuilder()
builder.ConnectionString = "Data Source=C:\Sample.mdb"
' Call the Add method to explicitly add key/value
' pairs to the internal collection.
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
builder.Add("Jet OLEDB:Database Password", "MyPassword!")
builder.Add("Jet OLEDB:System Database", "C:\Workgroup.mdb")
' Set up row-level locking.
builder.Add("Jet OLEDB:Database Locking Mode", 1)
' Dump the contents of the "filled-in" OleDbConnectionStringBuilder
' to the console window.
DumpBuilderContents(builder)
' Clear current values and reset known keywords to their
' default values.
builder.Clear()
' Dump the contents of the newly emptied
' OleDbConnectionStringBuilder
' to the console window.
DumpBuilderContents(builder)
Console.WriteLine("Press Enter to continue.")
Console.ReadLine()
End Sub
Private Sub DumpBuilderContents(ByVal builder As OleDbConnectionStringBuilder)
Console.WriteLine("=================")
Console.WriteLine("builder.ConnectionString = " & builder.ConnectionString)
For Each key As String In builder.Keys
Console.WriteLine(key & "=" & builder.Item(key).ToString)
Next
End Sub
End Module
Remarks
The Clear method removes all key/value pairs from the OleDbConnectionStringBuilder, and resets all corresponding properties to their default values. This includes setting the Count property to 0, and setting the ConnectionString property to an empty string.