OleDbConnectionStringBuilder.Clear 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
清除 OleDbConnectionStringBuilder 執行個體的內容。
public:
override void Clear();
public override void Clear ();
override this.Clear : unit -> unit
Public Overrides Sub Clear ()
範例
下列範例示範呼叫 Clear 方法的效果。 此範例會以某些索引鍵/值組填入 OleDbConnectionStringBuilder,然後呼叫 Clear 方法並顯示結果。
注意
這個範例包含了密碼,可示範 OleDbConnectionStringBuilder 如何搭配連接字串使用。 我們建議在您的應用程式中使用 Windows 驗證。 如果您必須使用密碼,請勿在您的應用程式中包含硬式編碼的密碼。
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
備註
方法 Clear 會從 OleDbConnectionStringBuilder移除所有索引鍵/值組,並將所有對應的屬性重設為預設值。 其中包括將 Count 屬性設定為 0 以及將 ConnectionString 屬性設定為空字串。