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

要获取或设置的项的键。

属性值

与指定键关联的值。 如果未找到指定的键,则尝试获取它会引发一个 ArgumentException,并尝试设置它使用指定的键创建一个新元素。

在 Visual Basic 中传递 null (Nothing) 键将引发 ArgumentNullException。 分配 null 值会删除键/值对。

属性

例外

集合中尚未设置该值 keyword

keyword是空引用(Visual Basic 中的 Nothing)。

该属性已设置,并且 DbConnectionStringBuilder 为只读。

-或-

该属性已设置, keyword 集合中不存在,并且 DbConnectionStringBuilder 大小固定。

示例

以下控制台应用程序使用 DbConnectionStringBuilder 属性创建一个新的 Item[],并将键/值对添加到其连接字符串。

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

注解

设置此属性时,如果字典中已存在指定的键,则会替换该值;否则,将创建一个新元素。

适用于

另请参阅