共用方式為


DbConnectionStringBuilder.Item[String] 屬性

定義

取得或設定與指定之索引鍵相關聯的值。

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

參數

keyword
String

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

屬性值

與指定之索引鍵關聯的值。 如果找不到指定的索引鍵,嘗試使其傳回 null 參考 (在 Visual Basic 中為 Nothing),並且嘗試將其設定為使用指定的索引鍵建立新項目。

傳遞 null (在 Visual Basic 中為 Nothing) 索引鍵會擲回 ArgumentNullException。 指派 null 值會移除索引鍵/值組。

屬性

例外狀況

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

屬性已設定,而且 DbConnectionStringBuilder 是唯讀的。

-或-

屬性已設定、keyword 不在於集合中,而且 DbConnectionStringBuilder 的大小固定。

範例

下列主控台應用程式會使用 Item[] 屬性,建立新的 DbConnectionStringBuilder ,並將索引鍵/值組新增至其 連接字串。

static void Main()
{
    DbConnectionStringBuilder builder = new
        DbConnectionStringBuilder();
    builder["Data Source"] = "(local)";

    // Note that Item is the indexer, so
    // you do not include it in the reference.
    builder["integrated security"] = true;
    builder["Initial Catalog"] = "AdventureWorks";

    // Overwrite the existing value for the Data Source key,
    // because it already exists within the collection.
    builder["Data Source"] = ".";

    Console.WriteLine(builder.ConnectionString);
    Console.WriteLine();
    Console.WriteLine("Press Enter to continue.");
    Console.ReadLine();
}
Module Module1

    Sub Main()
        Dim builder As New DbConnectionStringBuilder
        builder.Item("Data Source") = "(local)"

        ' Item is the default property, so 
        ' you need not include it in the reference.
        builder("integrated security") = True
        builder.Item("Initial Catalog") = "AdventureWorks"

        ' Overwrite the existing value for the data source value, 
        ' because it already exists within the collection.
        builder.Item("Data Source") = "."

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

備註

當您設定這個屬性時,如果指定的索引鍵已存在於字典中,則會取代值;否則會建立新的專案。

適用於

另請參閱