DbConnectionStringBuilder.Keys Property

Definition

Gets an ICollection that contains the keys in the DbConnectionStringBuilder.

C#
public virtual System.Collections.ICollection Keys { get; }
C#
[System.ComponentModel.Browsable(false)]
public virtual System.Collections.ICollection Keys { get; }

Property Value

An ICollection that contains the keys in the DbConnectionStringBuilder.

Implements

Attributes

Examples

The following console application example creates a new DbConnectionStringBuilder, and adds some keys. The code loops through the ICollection returned by the Keys property displaying the key/value pairs, and then adds a new key. Because the Keys property returns a dynamic ICollection, the second loop displays all the key/value pairs, including the newest item.

C#
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();
}

Remarks

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.

The returned ICollection is not a static copy; instead, the ICollection refers back to the keys in the original DbConnectionStringBuilder. Therefore, changes to the DbConnectionStringBuilder are reflected in the ICollection.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

See also