Export (0) Print
Expand All

OleDbConnectionStringBuilder Class

Provides a simple way to create and manage the contents of connection strings used by the OleDbConnection class.

System.Object
  System.Data.Common.DbConnectionStringBuilder
    System.Data.OleDb.OleDbConnectionStringBuilder

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

public sealed class OleDbConnectionStringBuilder : DbConnectionStringBuilder

The OleDbConnectionStringBuilder type exposes the following members.

  NameDescription
Public methodOleDbConnectionStringBuilder()Initializes a new instance of the OleDbConnectionStringBuilder class.
Public methodOleDbConnectionStringBuilder(String)Initializes a new instance of the OleDbConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information.
Top

  NameDescription
Public propertyBrowsableConnectionStringGets or sets a value that indicates whether the ConnectionString property is visible in Visual Studio designers. (Inherited from DbConnectionStringBuilder.)
Public propertyConnectionStringGets or sets the connection string associated with the DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder.)
Public propertyCountGets the current number of keys that are contained within the ConnectionString property. (Inherited from DbConnectionStringBuilder.)
Public propertyDataSourceGets or sets the name of the data source to connect to.
Public propertyFileNameGets or sets the name of the Universal Data Link (UDL) file for connecting to the data source.
Public propertyIsFixedSizeGets a value that indicates whether the DbConnectionStringBuilder has a fixed size. (Inherited from DbConnectionStringBuilder.)
Public propertyIsReadOnlyGets a value that indicates whether the DbConnectionStringBuilder is read-only. (Inherited from DbConnectionStringBuilder.)
Public propertyItemGets or sets the value associated with the specified key. In C#, this property is the indexer. (Overrides DbConnectionStringBuilder.Item.)
Public propertyKeysGets an ICollection that contains the keys in the OleDbConnectionStringBuilder. (Overrides DbConnectionStringBuilder.Keys.)
Public propertyOleDbServicesGets or sets the value to be passed for the OLE DB Services key within the connection string.
Public propertyPersistSecurityInfoGets or sets a Boolean value that indicates whether security-sensitive information, such as the password, is returned as part of the connection if the connection is open or has ever been in an open state.
Public propertyProviderGets or sets a string that contains the name of the data provider associated with the internal connection string.
Public propertyValuesGets an ICollection that contains the values in the DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder.)
Top

  NameDescription
Public methodAddAdds an entry with the specified key and value into the DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder.)
Public methodClearClears the contents of the OleDbConnectionStringBuilder instance. (Overrides DbConnectionStringBuilder.Clear().)
Protected methodClearPropertyDescriptorsClears the collection of PropertyDescriptor objects on the associated DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder.)
Public methodContainsKeyDetermines whether the OleDbConnectionStringBuilder contains a specific key. (Overrides DbConnectionStringBuilder.ContainsKey(String).)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodEquivalentToCompares the connection information in this DbConnectionStringBuilder object with the connection information in the supplied object. (Inherited from DbConnectionStringBuilder.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodRemoveRemoves the entry with the specified key from the OleDbConnectionStringBuilder instance. (Overrides DbConnectionStringBuilder.Remove(String).)
Public methodShouldSerializeIndicates whether the specified key exists in this DbConnectionStringBuilder instance. (Inherited from DbConnectionStringBuilder.)
Public methodToStringReturns the connection string associated with this DbConnectionStringBuilder. (Inherited from DbConnectionStringBuilder.)
Public methodTryGetValueRetrieves a value corresponding to the supplied key from the OleDbConnectionStringBuilder instance. (Overrides DbConnectionStringBuilder.TryGetValue(String, Object).)
Top

  NameDescription
Explicit interface implemetationPrivate methodICollection.CopyToCopies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedGets a value indicating whether access to the ICollection is synchronized (thread safe). (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate propertyICollection.SyncRootGets an object that can be used to synchronize access to the ICollection. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetAttributesReturns a collection of custom attributes for this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetClassNameReturns the class name of this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetComponentNameReturns the name of this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetConverterReturns a type converter for this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetDefaultEventReturns the default event for this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetDefaultPropertyReturns the default property for this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetEditorReturns an editor of the specified type for this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetEvents()Returns the events for this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetEvents(Attribute[])Returns the events for this instance of a component using the specified attribute array as a filter. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetProperties()Returns the properties for this instance of a component. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetProperties(Attribute[])Returns the properties for this instance of a component using the attribute array as a filter. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodICustomTypeDescriptor.GetPropertyOwnerReturns an object that contains the property described by the specified property descriptor. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodIDictionary.AddAdds an element with the provided key and value to the IDictionary object. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodIDictionary.ContainsDetermines whether the IDictionary object contains an element with the specified key. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodIDictionary.GetEnumeratorReturns an IDictionaryEnumerator object for the IDictionary object. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate propertyIDictionary.ItemGets or sets the element with the specified key. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodIDictionary.RemoveRemoves the element with the specified key from the IDictionary object. (Inherited from DbConnectionStringBuilder.)
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorReturns an enumerator that iterates through a collection. (Inherited from DbConnectionStringBuilder.)
Top

The connection string builder lets developers programmatically create syntactically correct connection strings, and parse and rebuild existing connection strings, using properties and methods of the class. The connection string builder provides strongly typed properties corresponding to the known key/value pairs allowed by OLE DB connections, and developers can add arbitrary key/value pairs for any other connection string values. The OleDbConnectionStringBuilder class implements the ICustomTypeDescriptor interface. This means that the class works with Visual Studio .NET designers at design time. When developers use the designer to build strongly typed DataSets and strongly typed connections within Visual Studio .NET, the strongly typed connection string builder class will display the properties associated with its type and will also have converters that can map common values for known keys.

Developers needing to create connection strings as part of applications can use the OleDbConnectionStringBuilder class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file. The OleDbConnectionStringBuilder performs checks only for the limited set of known key/value pairs. Therefore, this class can be used to create invalid connection strings. The following table lists the known keys and their corresponding properties within the OleDbConnectionStringBuilder class, and their default values. Besides these specific values, developers can add any key/value pairs to the collection that is contained within the OleDbConnectionStringBuilder instance:

Key

Property

Default value

File Name

FileName

""

Provider

Provider

""

Data Source

DataSource

""

Persist Security Info

PersistSecurityInfo

False

OLE DB Services

OleDbServices

-13

The Item property handles attempts to insert malicious entries. For example, the following code, using the default Item property (the indexer, in C#) correctly escapes the nested key/value pair:

System.Data.OleDb.OleDbConnectionStringBuilder builder = 
    new System.Data.OleDb.OleDbConnectionStringBuilder();
builder["Provider"] = "Microsoft.Jet.OLEDB.4.0";
builder["Data Source"] = "C:\\Sample.mdb";
builder["User Id"] = "Admin;NewValue=Bad";

The result is the following connection string that handles the invalid value in a safe manner:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Sample.mdb;User ID="Admin;NewValue=Bad"

The following console application builds connection strings for several OLE DB databases. First, the example creates a connection string for a Microsoft Access database, and then creates a connection string for an IBM DB2 database. The example also parses an existing connection string, and demonstrates various ways of manipulating the connection string's contents.

NoteNote

This example includes a password to demonstrate how OleDbConnectionStringBuilder works with connection strings. In your applications, we recommend that you use Windows Authentication. If you must use a password, do not include a hard-coded password in your application.

using System.Data.OleDb;

class Program
{
    static void Main(string[] args)
    {
        OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
        builder.ConnectionString = @"Data Source=C:\Sample.mdb";

        // Call the Add method to explicitly add key/value 
        // pairs to the internal collection.
        builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
        builder.Add("Jet OLEDB:Database Password", "MyPassword!");
        builder.Add("Jet OLEDB:System Database", @"C:\Workgroup.mdb");

        // Set up row-level locking.
        builder.Add("Jet OLEDB:Database Locking Mode", 1);

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

        // Clear current values and reset known keys to their 
        // default values.
        builder.Clear();

        // Pass the OleDbConnectionStringBuilder an existing  
        // connection string, and you can retrieve and 
        // modify any of the elements.
        builder.ConnectionString =
            "Provider=DB2OLEDB;Network Transport Library=TCPIP;" +
            "Network Address=192.168.0.12;Initial Catalog=DbAdventures;" +
            "Package Collection=SamplePackage;Default Schema=SampleSchema;";

        Console.WriteLine("Network Address = " + builder["Network Address"].ToString());
        Console.WriteLine();

        // Modify existing items.
        builder["Package Collection"] = "NewPackage";
        builder["Default Schema"] = "NewSchema";

        // Call the Remove method to remove items from  
        // the collection of key/value pairs.
        builder.Remove("User ID");

        // Note that calling Remove on a nonexistent item does not 
        // throw an exception.
        builder.Remove("BadItem");
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Setting the indexer adds the value, if  
        // necessary.
        builder["User ID"] = "SampleUser";
        builder["Password"] = "SamplePassword";
        Console.WriteLine(builder.ConnectionString);

        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
}

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft