DbConnectionStringBuilder.Keys 属性

定义

获取包含 ICollection 中的键的 DbConnectionStringBuilderGets an ICollection that contains the keys in the DbConnectionStringBuilder.

public:
 virtual property System::Collections::ICollection ^ Keys { System::Collections::ICollection ^ get(); };
public virtual System.Collections.ICollection Keys { get; }
[System.ComponentModel.Browsable(false)]
public virtual System.Collections.ICollection Keys { get; }
member this.Keys : System.Collections.ICollection
[<System.ComponentModel.Browsable(false)>]
member this.Keys : System.Collections.ICollection
Public Overridable ReadOnly Property Keys As ICollection

属性值

ICollection

一个 ICollection,它包含 DbConnectionStringBuilder 中的键。An ICollection that contains the keys in the DbConnectionStringBuilder.

实现

属性

示例

下面的控制台应用程序示例创建一个新的 DbConnectionStringBuilder ,并添加一些键。The following console application example creates a new DbConnectionStringBuilder, and adds some keys. 代码循环通过 ICollection Keys 显示键/值对的属性返回的,然后添加新的键。The code loops through the ICollection returned by the Keys property displaying the key/value pairs, and then adds a new key. 由于 Keys 属性返回动态,因此 ICollection 第二个循环显示所有键/值对,包括最新的项。Because the Keys property returns a dynamic ICollection, the second loop displays all the key/value pairs, including the newest item.

static void Main()
{
    DbConnectionStringBuilder builder = new
        DbConnectionStringBuilder();
    builder["Data Source"] = "(local)";
    builder["Integrated Security"] = true;
    builder["Initial Catalog"] = "AdventureWorks";

    // Obtain reference to the collection of keys.
    ICollection keys = builder.Keys;

    Console.WriteLine("Keys before adding TimeOut:");
    foreach (string key in keys)
        Console.WriteLine("{0}={1}", key, builder[key]);

    // Add a new item to the collection.
    builder["Timeout"] = 300;

    Console.WriteLine();
    Console.WriteLine("Keys after adding TimeOut:");

    // Because the Keys property is dynamically updated,
    // the following loop includes the Timeout key.
    foreach (string key in keys)
        Console.WriteLine("{0}={1}", key, builder[key]);
    Console.WriteLine();
    Console.WriteLine("Press Enter to continue.");
    Console.ReadLine();
}
Sub Main()
    Dim builder As New DbConnectionStringBuilder
    builder("Data Source") = "(local)"
    builder("integrated security") = True
    builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"

    ' Obtain reference to the collection of keys.
    Dim keys As ICollection = builder.Keys

    Console.WriteLine("Keys before adding TimeOut:")
    For Each key As String In keys
        Console.WriteLine("{0}={1}", key, builder(key))
    Next

    ' Add a new item to the collection.
    builder("Timeout") = 300

    Console.WriteLine()
    Console.WriteLine("Keys after adding TimeOut:")

    ' Because the Keys property is dynamically updated, 
    ' the following loop includes the Timeout key.
    For Each key As String In keys
        Console.WriteLine("{0}={1}", key, builder(key))
    Next

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

注解

ICollection 中的值的顺序未指定,但与 ICollection 属性返回的 Values 中的关联值顺序相同。The order of the values in the ICollection is unspecified, but it is the same order as the associated values in the ICollection returned by the Values property.

返回的不 ICollection 是静态副本; 相反,引用的是 ICollection 原始中的键 DbConnectionStringBuilderThe returned ICollection is not a static copy; instead, the ICollection refers back to the keys in the original DbConnectionStringBuilder. 因此,对的更改 DbConnectionStringBuilder 会反映在中 ICollectionTherefore, changes to the DbConnectionStringBuilder are reflected in the ICollection.

适用于

另请参阅