OleDbConnectionStringBuilder.Item[String] 屬性

定義

取得或設定與指定之索引鍵相關聯的值。 在 C# 中,這個屬性是索引子 (Indexer)。

public:
 virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ keyword); void set(System::String ^ keyword, System::Object ^ value); };
public override object this[string keyword] { get; set; }
member this.Item(string) : obj with get, set
Default Public Overrides Property Item(keyword As String) As Object

參數

keyword
String

要取得或設定之項目的索引鍵。

屬性值

與指定之索引鍵關聯的值。

例外狀況

連接字串的格式錯誤 (也許在索引鍵/值組中遺漏了必要的 "=")。

keyword 為 null 參考 (在 Visual Basic 中為 Nothing)。

範例

下列範例會 Item[] 使用 C#) 中的索引器 (屬性,以擷取和設定索引鍵/值組集合中的值。 請注意,在此情況下,設定提供者也會針對與選取提供者相關聯的所有索引鍵/值組提供預設值。

using System.Data.OleDb;

class Program
{
    static void Main()
    {
        OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
        builder.Provider = "Microsoft.Jet.Oledb.4.0";
        builder.DataSource = @"C:\Sample.mdb";
        // Set properties using the Item property (the indexer, in C#).
        builder["Jet OLEDB:Database Password"] = "DataPassword";
        builder["Jet OLEDB:Encrypt Database"] = true;
        builder["Jet OLEDB:System database"] = @"C:\Workgroup.mdw";

        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Use the Item property to retrieve values as well.
        Console.WriteLine(builder["Jet OLEDB:System database"]);
        Console.WriteLine(builder["Jet OLEDB:Encrypt Database"]);

        // You can set or retrieve any of the "default" values for the
        // provider, even if you didn't set their values.
        Console.WriteLine(builder["Jet OLEDB:Database Locking Mode"]);
        Console.WriteLine(builder["Jet OLEDB:Global Partial Bulk Ops"]);

        Console.WriteLine("Press Enter to continue.");
        Console.ReadLine();
    }
}
Imports System.Data.OleDb    

Module Module1
  Sub Main()
    Dim builder As New OleDbConnectionStringBuilder
    builder.Provider = "Microsoft.Jet.Oledb.4.0"
    builder.DataSource = "C:\Sample.mdb"
    ' Set properties using the Item property.
    builder.Item("Jet OLEDB:Database Password") = "DataPassword"
    builder.Item("Jet OLEDB:Encrypt Database") = True

    ' Because Item is the default property, you can leave out
    ' the explicit reference.
    builder("Jet OLEDB:System database") = "C:\Workgroup.mdw"

    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine()

    ' Use the Item property to retrieve values, as well.
    Console.WriteLine(builder.Item("Jet OLEDB:System database"))
    Console.WriteLine(builder("Jet OLEDB:Encrypt Database"))

    ' You can set or retrieve any of the "default" values for the 
    ' provider, as well, even if you did not set their values. Again, 
    ' explicitly specifying the Item property name is optional.
    Console.WriteLine(builder.Item("Jet OLEDB:Database Locking Mode"))
    Console.WriteLine(builder("Jet OLEDB:Global Partial Bulk Ops"))

    Console.WriteLine("Press Enter to continue.")
    Console.ReadLine()
  End Sub
End Module

備註

由於設定 Provider 屬性可能會根據特定提供者) 的行為,將對應的專案新增至索引鍵/值組的集合 (,因此您可以擷取尚未明確設定之索引鍵的值。 例如,一旦您已將 Provider 屬性設定為 「sqloledb」,即使您尚未自行設定,您也可以擷取 「工作站標識碼」 值。 如需示範,請參閱本主題中的範例。

適用於

另請參閱