OdbcConnectionStringBuilder OdbcConnectionStringBuilder OdbcConnectionStringBuilder OdbcConnectionStringBuilder Class

Definition

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

public ref class OdbcConnectionStringBuilder sealed : System::Data::Common::DbConnectionStringBuilder
[System.ComponentModel.TypeConverter(typeof(System.Data.Odbc.OdbcConnectionStringBuilder/OdbcConnectionStringBuilderConverter))]
public sealed class OdbcConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
type OdbcConnectionStringBuilder = class
    inherit DbConnectionStringBuilder
Public NotInheritable Class OdbcConnectionStringBuilder
Inherits DbConnectionStringBuilder
Inheritance
OdbcConnectionStringBuilderOdbcConnectionStringBuilderOdbcConnectionStringBuilderOdbcConnectionStringBuilder
Attributes

Examples

The following console application builds connection strings for several ODBC databases. First, the example creates a connection string for a Microsoft Access database. It 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 contents of the connection string.

Note

This example includes a password to demonstrate how OdbcConnectionStringBuilder 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.Odbc;

class Program
{
    static void Main()
    {
        OdbcConnectionStringBuilder builder = 
            new OdbcConnectionStringBuilder();
        builder.Driver = "Microsoft Access Driver (*.mdb)";

        // Call the Add method to explicitly add key/value
        // pairs to the internal collection.
        builder.Add("Dbq", "C:\\info.mdb");
        builder.Add("Uid", "Admin");
        builder.Add("Pwd", "pass!word1");

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

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

        // Pass the OdbcConnectionStringBuilder an existing 
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString =
            "driver={IBM DB2 ODBC DRIVER};Database=SampleDB;" +
            "hostname=SampleServerName;port=SamplePortNum;" +
            "protocol=TCPIP;uid=Admin;pwd=pass!word1";

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

        // Modify existing items.
        builder["uid"] = "NewUser";
        builder["pwd"] = "Pass@word2";

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

        // 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 associated value, if 
        // necessary.
        builder["NewKey"] = "newValue";
        Console.WriteLine(builder.ConnectionString);

        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
}
Imports System.Data.Odbc    

Module Module1
  Sub Main()
    Dim builder As New OdbcConnectionStringBuilder()
    builder.Driver = "Microsoft Access Driver (*.mdb)"

    ' Call the Add method to explicitly add key/value
    ' pairs to the internal collection.
    builder.Add("Dbq", "C:\info.mdb")
    builder.Add("Uid", "Admin")
    builder.Add("Pwd", "pass!word1")

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

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

    ' Pass the OdbcConnectionStringBuilder an existing 
    ' connection string, and you can retrieve and
    ' modify any of the elements.
    builder.ConnectionString = _
        "driver={IBM DB2 ODBC DRIVER};Database=SampleDB;" & _
        "hostname=SampleServerName;port=SamplePortNum;" & _
        "protocol=TCPIP;uid=Admin;pwd=pass!word1"

    Console.WriteLine("protocol = " & builder("protocol").ToString())
    Console.WriteLine()

    ' Modify existing items:
    builder("uid") = "NewUser"
    builder("pwd") = "Pass@word2"

    ' Call the Remove method to remove items from 
    ' the collection of key/value pairs.
    builder.Remove("port")

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

    ' The Item property is the default for the class, 
    ' and setting the Item property adds the value, if 
    ' necessary.
    builder("NewKey") = "newValue"
    Console.WriteLine(builder.ConnectionString)

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

Remarks

The connection string builders let 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 ODBC connections, and developers can add arbitrary key/value pairs for any other connection string values.

Developers needing to create connection strings as part of applications can use the OdbcConnectionStringBuilder class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file. OdbcConnectionStringBuilder 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 specific known keys together with their corresponding properties within the OdbcConnectionStringBuilder class, and their default values. Besides these specific values, developers can add any key/value pairs to the collection that is contained within the OdbcConnectionStringBuilder instance.

Key Property Comment Default value
Driver Driver Developers should not include the braces surrounding the driver name when they set the Driver property. The OdbcConnectionStringBuilder instance adds braces as needed. Empty string
DSN Dsn Empty string

If any value (other than the Driver value) within the connection string contains a semicolon (;), the OdbcConnectionStringBuilder surrounds the value with quotation marks in the connection string. In order to avoid this issue with the Driver value that frequently contains a semicolon, the OdbcConnectionStringBuilder class always surrounds this value with braces. The ODBC specification indicates that driver values that contain semicolons must be surrounded with braces, and this class handles this for you.

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

Dim builder As _  
 New System.Data.Odbc.OdbcConnectionStringBuilder  
' Take advantage of the Driver property.   
builder.Driver = "SQL Server"  
builder("Server") = "MyServer;NewValue=Bad"  
Console.WriteLine(builder.ConnectionString)  
System.Data.Odbc.OdbcConnectionStringBuilder builder =   
  new System.Data.Odbc.OdbcConnectionStringBuilder();  
// Take advantage of the Driver property.   
builder.Driver = "SQL Server";  
builder["Server"] = "MyServer;NewValue=Bad";  
Console.WriteLine(builder.ConnectionString);  

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

Driver={SQL Server};Server="MyServer;NewValue=Bad"  

Constructors

OdbcConnectionStringBuilder() OdbcConnectionStringBuilder() OdbcConnectionStringBuilder() OdbcConnectionStringBuilder()

Initializes a new instance of the OdbcConnectionStringBuilder class.

OdbcConnectionStringBuilder(String) OdbcConnectionStringBuilder(String) OdbcConnectionStringBuilder(String) OdbcConnectionStringBuilder(String)

Initializes a new instance of the OdbcConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information.

Properties

BrowsableConnectionString BrowsableConnectionString BrowsableConnectionString BrowsableConnectionString

Gets or sets a value that indicates whether the ConnectionString property is visible in Visual Studio designers.

(Inherited from DbConnectionStringBuilder)
ConnectionString ConnectionString ConnectionString ConnectionString

Gets or sets the connection string associated with the DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
Count Count Count Count

Gets the current number of keys that are contained within the ConnectionString property.

(Inherited from DbConnectionStringBuilder)
Driver Driver Driver Driver

Gets or sets the name of the ODBC driver associated with the connection.

Dsn Dsn Dsn Dsn

Gets or sets the name of the data source name (DSN) associated with the connection.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

Gets a value indicating whether access to the ICollection is synchronized (thread safe).

(Inherited from DbConnectionStringBuilder)
ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

Gets an object that can be used to synchronize access to the ICollection.

(Inherited from DbConnectionStringBuilder)
IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize Inherited from DbConnectionStringBuilder
IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly Inherited from DbConnectionStringBuilder
IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object]

Gets or sets the element with the specified key.

(Inherited from DbConnectionStringBuilder)
IsFixedSize IsFixedSize IsFixedSize IsFixedSize

Gets a value that indicates whether the DbConnectionStringBuilder has a fixed size.

(Inherited from DbConnectionStringBuilder)
IsReadOnly IsReadOnly IsReadOnly IsReadOnly

Gets a value that indicates whether the DbConnectionStringBuilder is read-only.

(Inherited from DbConnectionStringBuilder)
Item[String] Item[String] Item[String] Item[String]

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

Keys Keys Keys Keys

Gets an ICollection that contains the keys in the OdbcConnectionStringBuilder.

Values Values Values Values

Gets an ICollection that contains the values in the DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)

Methods

Add(String, Object) Add(String, Object) Add(String, Object) Add(String, Object)

Adds an entry with the specified key and value into the DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
Clear() Clear() Clear() Clear()

Clears the contents of the OdbcConnectionStringBuilder instance.

ClearPropertyDescriptors() ClearPropertyDescriptors() ClearPropertyDescriptors() ClearPropertyDescriptors()

Clears the collection of PropertyDescriptor objects on the associated DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
ContainsKey(String) ContainsKey(String) ContainsKey(String) ContainsKey(String)

Determines whether the OdbcConnectionStringBuilder contains a specific key.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
EquivalentTo(DbConnectionStringBuilder) EquivalentTo(DbConnectionStringBuilder) EquivalentTo(DbConnectionStringBuilder) EquivalentTo(DbConnectionStringBuilder)

Compares the connection information in this DbConnectionStringBuilder object with the connection information in the supplied object.

(Inherited from DbConnectionStringBuilder)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetProperties(Hashtable) GetProperties(Hashtable) GetProperties(Hashtable) GetProperties(Hashtable)

Fills a supplied Hashtable with information about all the properties of this DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(String) Remove(String) Remove(String) Remove(String)

Removes the entry with the specified key from the OdbcConnectionStringBuilder instance.

ShouldSerialize(String) ShouldSerialize(String) ShouldSerialize(String) ShouldSerialize(String)

Indicates whether the specified key exists in this DbConnectionStringBuilder instance.

(Inherited from DbConnectionStringBuilder)
ToString() ToString() ToString() ToString()

Returns the connection string associated with this DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
TryGetValue(String, Object) TryGetValue(String, Object) TryGetValue(String, Object) TryGetValue(String, Object)

Retrieves a value corresponding to the supplied key from this OdbcConnectionStringBuilder.

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

Copies the elements of the ICollection to an Array, starting at a particular Array index.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetAttributes() ICustomTypeDescriptor.GetAttributes() ICustomTypeDescriptor.GetAttributes() ICustomTypeDescriptor.GetAttributes()

Returns a collection of custom attributes for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetClassName() ICustomTypeDescriptor.GetClassName() ICustomTypeDescriptor.GetClassName() ICustomTypeDescriptor.GetClassName()

Returns the class name of this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetComponentName() ICustomTypeDescriptor.GetComponentName() ICustomTypeDescriptor.GetComponentName() ICustomTypeDescriptor.GetComponentName()

Returns the name of this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetConverter() ICustomTypeDescriptor.GetConverter() ICustomTypeDescriptor.GetConverter() ICustomTypeDescriptor.GetConverter()

Returns a type converter for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetDefaultEvent() ICustomTypeDescriptor.GetDefaultEvent() ICustomTypeDescriptor.GetDefaultEvent() ICustomTypeDescriptor.GetDefaultEvent()

Returns the default event for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetDefaultProperty() ICustomTypeDescriptor.GetDefaultProperty() ICustomTypeDescriptor.GetDefaultProperty() ICustomTypeDescriptor.GetDefaultProperty()

Returns the default property for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEditor(Type) ICustomTypeDescriptor.GetEditor(Type) ICustomTypeDescriptor.GetEditor(Type) ICustomTypeDescriptor.GetEditor(Type)

Returns an editor of the specified type for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEvents() ICustomTypeDescriptor.GetEvents() ICustomTypeDescriptor.GetEvents() ICustomTypeDescriptor.GetEvents()

Returns the events for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEvents(Attribute[]) ICustomTypeDescriptor.GetEvents(Attribute[]) ICustomTypeDescriptor.GetEvents(Attribute[]) ICustomTypeDescriptor.GetEvents(Attribute[])

Returns the events for this instance of a component using the specified attribute array as a filter.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetProperties() ICustomTypeDescriptor.GetProperties() ICustomTypeDescriptor.GetProperties() ICustomTypeDescriptor.GetProperties()

Returns the properties for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetProperties(Attribute[]) ICustomTypeDescriptor.GetProperties(Attribute[]) ICustomTypeDescriptor.GetProperties(Attribute[]) ICustomTypeDescriptor.GetProperties(Attribute[])

Returns the properties for this instance of a component using the attribute array as a filter.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor) ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor) ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor) ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor)

Returns an object that contains the property described by the specified property descriptor.

(Inherited from DbConnectionStringBuilder)
IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object)

Adds an element with the provided key and value to the IDictionary object.

(Inherited from DbConnectionStringBuilder)
IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object)

Determines whether the IDictionary object contains an element with the specified key.

(Inherited from DbConnectionStringBuilder)
IDictionary.GetEnumerator() IDictionary.GetEnumerator() IDictionary.GetEnumerator() IDictionary.GetEnumerator()

Returns an IDictionaryEnumerator object for the IDictionary object.

(Inherited from DbConnectionStringBuilder)
IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object)

Removes the element with the specified key from the IDictionary object.

(Inherited from DbConnectionStringBuilder)
IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

(Inherited from DbConnectionStringBuilder)

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See also