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

要获取或设置的项的键。

属性值

与指定的键相关联的值。 如果指定键未找到,尝试让其返回空引用(在 Visual Basic 中为 Nothing),并尝试设置为使用指定键创建新的元素。

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

属性

例外

keyword 为空引用(在 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

注解

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

适用于

另请参阅