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
public sealed class OdbcConnectionStringBuilder : 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
[<System.ComponentModel.TypeConverter(typeof(System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter))>]
type OdbcConnectionStringBuilder = class
inherit DbConnectionStringBuilder
Public NotInheritable Class OdbcConnectionStringBuilder
Inherits DbConnectionStringBuilder
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.
using System.Data.Odbc;
class Program
{
static void Main()
{
OdbcConnectionStringBuilder builder = new()
{
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");
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";
Console.WriteLine($"protocol = {builder["protocol"].ToString()}");
Console.WriteLine();
// 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);
}
}
Imports System.Data.Odbc
Module Module1
Sub Main()
Dim builder As New OdbcConnectionStringBuilder With {
.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")
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"
Console.WriteLine("protocol = " & builder("protocol").ToString())
Console.WriteLine()
' 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
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 stringIf 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[] property handles attempts to insert malicious code. For example, the following code, using the default Item[] 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 Properties Methods Explicit Interface Implementations Extension Methods See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4