Edit

Share via


OleDbConnectionStringBuilder.Item[String] Property

Definition

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

public:
 virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ keyword); void set(System::String ^ keyword, System::Object ^ value); };
public override object this[string keyword] { get; set; }
member this.Item(string) : obj with get, set
Default Public Overrides Property Item(keyword As String) As Object

Parameters

keyword
String

The key of the item to get or set.

Property Value

The value associated with the specified key.

Exceptions

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

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

Examples

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: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();
    }
}
Imports System.Data.OleDb

Module Module1
  Sub Main()
    Dim builder As New OleDbConnectionStringBuilder
    builder.Provider = "Microsoft.Jet.Oledb.4.0"
    builder.DataSource = "C:\Sample.mdb"
    ' Set properties using the Item property.
    builder.Item("Jet OLEDB:Encrypt Database") = True

    ' Because Item is the default property, you can leave out
    ' the explicit reference.
    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.Item("Jet OLEDB:System database"))
    Console.WriteLine(builder("Jet OLEDB:Encrypt Database"))

    ' You can set or retrieve any of the "default" values for the
    ' provider, as well, even if you did not set their values. Again,
    ' explicitly specifying the Item property name is optional.
    Console.WriteLine(builder.Item("Jet OLEDB:Database Locking Mode"))
    Console.WriteLine(builder("Jet OLEDB:Global Partial Bulk Ops"))

    Console.WriteLine("Press Enter to continue.")
    Console.ReadLine()
  End Sub
End Module

Remarks

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.

Applies to

See also