DbConnectionStringBuilder.Item[String] 属性

定义

获取或设置与指定的键关联的值。Gets or sets the value associated with the specified key.

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

要获取或设置的项的键。The key of the item to get or set.

属性值

Object

与指定的键相关联的值。The value associated with the specified key. 如果指定键未找到,尝试让其返回空引用(在 Visual Basic 中为 Nothing),并尝试设置为使用指定键创建新的元素。If the specified key is not found, trying to get it returns a null reference (Nothing in Visual Basic), and trying to set it creates a new element using the specified key.

传递 null 键(在 Visual Basic 中为 Nothing)将引发 ArgumentNullExceptionPassing a null (Nothing in Visual Basic) key throws an ArgumentNullException. 分配一个删除键/值对的 null 值。Assigning a null value removes the key/value pair.

属性

例外

keyword 为空引用(在 Visual Basic 中为 Nothing)。keyword is a null reference (Nothing in Visual Basic).

设置了该属性,但 DbConnectionStringBuilder 为只读。The property is set, and the DbConnectionStringBuilder is read-only.

- 或 --or-

设置了该属性,但 keyword 不存在于集合中,且 DbConnectionStringBuilder 具有固定的大小。The property is set, keyword does not exist in the collection, and the DbConnectionStringBuilder has a fixed size.

示例

下面的控制台应用程序将创建一个新的 DbConnectionStringBuilder ,并使用属性将键/值对添加到它的连接字符串中 Item[]The following console application creates a new DbConnectionStringBuilder and adds key/value pairs to its connection string, using the Item[] property.

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

注解

设置此属性时,如果字典中已存在指定的键,则替换该值;否则,将创建一个新元素。When you set this property, if the specified key already exists in the dictionary, the value is replaced; otherwise, a new element is created.

适用于

另请参阅