OleDbConnectionStringBuilder 构造函数

定义

初始化 OleDbConnectionStringBuilder 类的新实例。

重载

OleDbConnectionStringBuilder()

初始化 OleDbConnectionStringBuilder 类的新实例。

OleDbConnectionStringBuilder(String)

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

OleDbConnectionStringBuilder()

Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs

初始化 OleDbConnectionStringBuilder 类的新实例。

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

另请参阅

适用于

OleDbConnectionStringBuilder(String)

Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs
Source:
OleDbConnectionStringBuilder.cs

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

public:
 OleDbConnectionStringBuilder(System::String ^ connectionString);
public OleDbConnectionStringBuilder (string? connectionString);
public OleDbConnectionStringBuilder (string connectionString);
new System.Data.OleDb.OleDbConnectionStringBuilder : string -> System.Data.OleDb.OleDbConnectionStringBuilder
Public Sub New (connectionString As String)

参数

connectionString
String

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

例外

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

示例

以下示例创建多个OleDbConnectionStringBuilder实例,在每种情况下将不同的连接字符串传递给构造函数。 请注意,设置与连接关联的提供程序如何更改对象集合中的预定义键/值对集。

注意

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

using System.Data.OleDb;

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("Provider=sqloledb;Data Source=(local);" +
                "Initial Catalog=AdventureWorks;" +
                "User Id=ab;Password=Password@1");

            // Create an Access connection string.
            DumpBuilderContents("Provider=Microsoft.Jet.OLEDB.4.0;" +
                @"Data Source=C:\Sample.mdb");

            // Create an Oracle connection string.
            DumpBuilderContents("Provider=msdaora;Data Source=SomeOracleDb;" +
                "User Id=userName;Password=Pass@word1;");

            // Create an Sybase connection string.
            DumpBuilderContents("Provider=ASAProv;Data source=myASA");

            Console.WriteLine("Press any key to finish.");
            Console.ReadLine();
        }
        catch (System.ArgumentException ex)
        {

            Console.WriteLine("Error: " + ex.Message);
        }
    }

    private static void DumpBuilderContents(string connectString)
    {
        OleDbConnectionStringBuilder builder =
            new OleDbConnectionStringBuilder(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.OleDb    

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("Provider=sqloledb;Data Source=(local);" & _
       "Initial Catalog=AdventureWorks;" & _
       "User Id=ab;Password=Password@1")

      ' Create an Access connection string.
      DumpBuilderContents("Provider=Microsoft.Jet.OLEDB.4.0;" & _
       "Data Source=C:\Sample.mdb")

      ' Create an Oracle connection string.
      DumpBuilderContents("Provider=msdaora;Data Source=SomeOracleDb;" & _
       "User Id=userName;Password=Pass@word1;")

      ' Create a Sybase connection string.
      DumpBuilderContents("Provider=ASAProv;Data source=myASA")

      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 OleDbConnectionStringBuilder(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

注解

Provider设置属性(通过设置ConnectionString属性) 直接 (,或者通过将连接字符串作为参数传递给构造函数)可能会影响实例中包含的OleDbConnectionStringBuilder键/值对集。 例如,将 Provider 属性设置为“sqloledb”会添加所有标准 SQL 连接字符串属性。 有关此行为的演示,请参阅本主题中的示例。

对于某些提供程序,在构造函数中OleDbConnectionStringBuilder分配连接字符串会导致提供键/值对的顺序重新排列。

另请参阅

适用于