System.Data.Odbc.OdbcConnectionStringBuilder Class

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

See Also: OdbcConnectionStringBuilder Members

Syntax

[System.ComponentModel.TypeConverter("System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
[System.ComponentModel.DefaultProperty("Driver")]
public sealed class OdbcConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder

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 System.Data.Odbc.OdbcConnectionStringBuilder class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file. System.Data.Odbc.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 System.Data.Odbc.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 System.Data.Odbc.OdbcConnectionStringBuilder instance.

Driver

OdbcConnectionStringBuilder.Driver

Developers should not include the braces surrounding the driver name when they set the OdbcConnectionStringBuilder.Driver property. The System.Data.Odbc.OdbcConnectionStringBuilder instance adds braces as needed.

Empty string

DSN

OdbcConnectionStringBuilder.Dsn

Empty string

If any value (other than the OdbcConnectionStringBuilder.Driver value) within the connection string contains a semicolon (;), the System.Data.Odbc.OdbcConnectionStringBuilder surrounds the value with quotation marks in the connection string. In order to avoid this issue with the OdbcConnectionStringBuilder.Driver value that frequently contains a semicolon, the System.Data.Odbc.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 OdbcConnectionStringBuilder.Item(string) property handles attempts to insert malicious code. For example, the following code, using the default OdbcConnectionStringBuilder.Item(string) property (the indexer, in C#) correctly escapes the nested key/value pair.

[Visual Basic]

Example

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)

[C#]

Example

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:

Example

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

Requirements

Namespace: System.Data.Odbc
Assembly: System.Data (in System.Data.dll)
Assembly Versions: 2.0.0.0