Provides a simple way to create and manage the contents of connection strings used by the OdbcConnection class.
Assembly: System.Data (in System.Data.dll)
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 class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file. 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 class, and their default values. Besides these specific values, developers can add any key/value pairs to the collection that is contained within the instance.
Developers should not include the braces surrounding the driver name when they set the Driver property. The instance adds braces as needed.
If any value (other than the Driver value) within the connection string contains a semicolon (;), the 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 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.
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:
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.
This example includes a password to demonstrate how 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.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.