OdbcConnectionStringBuilder 构造函数

定义

初始化 OdbcConnectionStringBuilder 类的新实例。

重载

OdbcConnectionStringBuilder()

初始化 OdbcConnectionStringBuilder 类的新实例。

OdbcConnectionStringBuilder(String)

初始化 OdbcConnectionStringBuilder 类的新实例。 所提供的连接字符串为实例的内部连接信息提供数据。

OdbcConnectionStringBuilder()

Source:
OdbcConnectionStringbuilder.cs
Source:
OdbcConnectionStringbuilder.cs

初始化 OdbcConnectionStringBuilder 类的新实例。

public:
 OdbcConnectionStringBuilder();
public OdbcConnectionStringBuilder ();
Public Sub New ()

另请参阅

适用于

OdbcConnectionStringBuilder(String)

Source:
OdbcConnectionStringbuilder.cs
Source:
OdbcConnectionStringbuilder.cs

初始化 OdbcConnectionStringBuilder 类的新实例。 所提供的连接字符串为实例的内部连接信息提供数据。

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)

参数

connectionString
String

对象内部连接信息的基础。 分析为键/值对。

例外

连接字符串的格式错误(可能是键/值对中缺少必需的“=”)。

示例

以下示例创建多个OdbcConnectionStringBuilder实例,在每种情况下将不同的连接字符串传递给构造函数。 请注意,检索 属性时,可能会修改连接字符串中元素的顺序ConnectionString。 另请注意,除预定义的“Dsn”和“Driver”键以外的键由 OdbcConnectionStringBuilder 类转换为小写。

注意

该示例包括一个密码以演示 OdbcConnectionStringBuilder 如何使用连接字符串。 在您的应用程序中,建议使用 Windows 身份验证。 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。

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

注解

可以在构造函数中传递连接字符串,也可以显式设置 ConnectionString 属性。 无论哪种方式,该行为都是相同的。

另请参阅

适用于