DbConnectionStringBuilder.Keys 属性    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取包含 ICollection 中的键的 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.ICollectionPublic Overridable ReadOnly Property Keys As ICollection属性值
一个 ICollection,它包含 DbConnectionStringBuilder 中的键。
实现
- 属性
示例
以下控制台应用程序示例创建新的 DbConnectionStringBuilder,并添加一些键。 代码遍历 ICollection 显示 Keys 键/值对的属性返回的 ,然后添加新键。 由于 属性 Keys 返回动态 ICollection,因此第二个循环显示所有键/值对,包括最新的项。
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 中的关联值顺序相同。
返回的 ICollection 不是静态副本;相反, ICollection 引用回原始 DbConnectionStringBuilder中的键。 因此,对 DbConnectionStringBuilder 的更改将反映在 中 ICollection。