OleDbConnectionStringBuilder.Item Property (String)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Data.OleDb
Assembly:  System.Data (in System.Data.dll)

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

Parameters

keyword
Type: System.String

The key of the item to get or set.

Property Value

Type: System.Object

The value associated with the specified key.

Exception Condition
ArgumentException

The connection string is incorrectly formatted (perhaps missing the required "=" within a key/value pair).

ArgumentNullException

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

Because setting the Provider property may add corresponding items to the collection of key/value pairs (depending on the behavior of the specific provider), you may be able to retrieve a value for a key you have not set explicitly. For example, as soon as you have set the Provider property to "sqloledb," you can retrieve the "Workstation ID" value even if you have not set it yourself. See the example in this topic for a demonstration.

The following example uses the Item property (the indexer, in C#) to retrieve and set values within the collection of key/value pairs. Note that setting the provider, in this case, also provides default values for all the key/value pairs associated with the selected provider.

using System.Data.OleDb;

class Program
{
    static void Main()
    {
        OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
        builder.Provider = "Microsoft.Jet.Oledb.4.0";
        builder.DataSource = @"C:\Sample.mdb";
        // Set properties using the Item property (the indexer, in C#).
        builder["Jet OLEDB:Database Password"] = "DataPassword";
        builder["Jet OLEDB:Encrypt Database"] = true;
        builder["Jet OLEDB:System database"] = @"C:\Workgroup.mdw";

        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Use the Item property to retrieve values as well.
        Console.WriteLine(builder["Jet OLEDB:System database"]);
        Console.WriteLine(builder["Jet OLEDB:Encrypt Database"]);

        // You can set or retrieve any of the "default" values for the 
        // provider, even if you didn't set their values.
        Console.WriteLine(builder["Jet OLEDB:Database Locking Mode"]);
        Console.WriteLine(builder["Jet OLEDB:Global Partial Bulk Ops"]);

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

    }
}

.NET Framework
Available since 2.0
Return to top
Show: