OdbcConnectionStringBuilder Constructors
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Initializes a new instance of the OdbcConnectionStringBuilder class.
Overloads
OdbcConnectionStringBuilder() |
Initializes a new instance of the OdbcConnectionStringBuilder class. |
OdbcConnectionStringBuilder(String) |
Initializes a new instance of the OdbcConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information. |
OdbcConnectionStringBuilder()
Initializes a new instance of the OdbcConnectionStringBuilder class.
public:
OdbcConnectionStringBuilder();
public OdbcConnectionStringBuilder ();
Public Sub New ()
See also
Applies to
OdbcConnectionStringBuilder(String)
Initializes a new instance of the OdbcConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information.
public:
OdbcConnectionStringBuilder(System::String ^ connectionString);
public OdbcConnectionStringBuilder (string? connectionString);
public OdbcConnectionStringBuilder (string connectionString);
new System.Data.Odbc.OdbcConnectionStringBuilder : string -> System.Data.Odbc.OdbcConnectionStringBuilder
Public Sub New (connectionString As String)
Parameters
- connectionString
- String
The basis for the object's internal connection information. Parsed into key/value pairs.
Exceptions
The connection string is incorrectly formatted (perhaps missing the required "=" within a key/value pair).
Examples
The following example creates multiple OdbcConnectionStringBuilder instances, passing a different connection string to the constructor in each case. Note that the ordering of elements within the connection string may be modified when you retrieve the ConnectionString property. Also note that keys other than the predefined "Dsn" and "Driver" keys are converted to lowercase by the OdbcConnectionStringBuilder class.
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()
{
try
{
// Build an empty instance, just to see
// the contents of the keys.
DumpBuilderContents("");
// Create a SQL Server connection string.
DumpBuilderContents("Driver={SQL Server};Server=(local);Database=AdventureWorks;Uid=ab;Pwd=pass@word1");
// Create an Access connection string.
DumpBuilderContents(@"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\info.mdb;Exclusive=1;Uid=admin;Pwd=pass@word1");
// Create an Oracle connection string.
DumpBuilderContents("Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=Admin;Pwd=pass@word1;");
// Create a Sybase connection string.
DumpBuilderContents("Driver={SYBASE ASE ODBC Driver};Srvr=SomeServer;Uid=admin;Pwd=pass@word1");
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
catch (System.ArgumentException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
private static void DumpBuilderContents(string connectString)
{
OdbcConnectionStringBuilder builder =
new OdbcConnectionStringBuilder(connectString);
Console.WriteLine("=================");
Console.WriteLine("Original connectString = " + connectString);
Console.WriteLine("builder.ConnectionString = " + builder.ConnectionString);
foreach (string key in builder.Keys)
{
Console.WriteLine(key + "=" + builder[key].ToString());
}
}
}
Imports System.Data.Odbc
Module Module1
Sub Main()
Try
' Build an empty instance, just to see
' the contents of the keys.
DumpBuilderContents("")
' Create a SQL Server connection string.
DumpBuilderContents("Driver={SQL Server};Server=(local);Database=AdventureWorks;Uid=ab;Pwd=pass@word1")
' Create an Access connection string.
DumpBuilderContents("Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\info.mdb;Exclusive=1;Uid=admin;Pwd=pass@word1")
' Create an Oracle connection string.
DumpBuilderContents("Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=Admin;Pwd=pass@word1;")
' Create a Sybase connection string.
DumpBuilderContents("Driver={SYBASE ASE ODBC Driver};Srvr=SomeServer;Uid=admin;Pwd=pass@word1")
Console.WriteLine("Press any key to finish.")
Console.ReadLine()
Catch ex As System.ArgumentException
Console.WriteLine("Error: " & ex.Message)
End Try
End Sub
Private Sub DumpBuilderContents(ByVal connectString As String)
Dim builder As New OdbcConnectionStringBuilder(connectString)
Console.WriteLine("=================")
Console.WriteLine("Original connectString = " & connectString)
Console.WriteLine("builder.ConnectionString = " & builder.ConnectionString)
For Each key As String In builder.Keys
Console.WriteLine(key & "=" & builder.Item(key).ToString)
Next
End Sub
End Module
Remarks
You can pass a connection string in the constructor, or you can set the ConnectionString property explicitly. The behavior is the same either way.