DbConnectionStringBuilder.Keys 属性

定义

获取包含 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.ICollection
Public Overridable ReadOnly Property Keys As ICollection

属性值

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

适用于

另请参阅