SqlConnectionStringBuilder.Item[String] Property

Definition

Gets or sets the value associated with the specified key. In C#, this property is the indexer.

C#
public override object this[string keyword] { get; set; }

Parameters

keyword
String

The key of the item to get or set.

Property Value

The value associated with the specified key.

Exceptions

keyword is a null reference (Nothing in Visual Basic).

Tried to add a key that does not exist within the available keys.

Invalid value within the connection string (specifically, a Boolean or numeric value was expected but not supplied).

Examples

The following code, in a console application, creates a new SqlConnectionStringBuilder and adds key/value pairs to its connection string, using the Item[] property.

C#
class Program
{
    static void Main()
    {
        SqlConnectionStringBuilder builder =
            new SqlConnectionStringBuilder();
        builder["Data Source"] = "(local)";
        builder["Integrated Security"] = true;
        builder["Initial Catalog"] = "AdventureWorks";

        // Overwrite the existing value for the Data Source value.
        builder["Data Source"] = ".";

        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();
        Console.WriteLine("Press Enter to continue.");
        Console.ReadLine();
    }
}

Remarks

Because the SqlConnectionStringBuilder contains a fixed-size dictionary, trying to add a key that does not exist within the dictionary throws a KeyNotFoundException.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.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 (package-provided)

See also