共用方式為


OleDbConnectionStringBuilder.Provider 屬性

定義

取得或設定字串,此字串包含與內部連接字串關聯的資料提供者 (Data Provider) 名稱。

public:
 property System::String ^ Provider { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbConnectionStringBuilder+OleDbProviderConverter))]
public string Provider { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbConnectionStringBuilder+OleDbProviderConverter))>]
member this.Provider : string with get, set
Public Property Provider As String

屬性值

Provider 屬性的值,如未提供任何值,則為 String.Empty

屬性

範例

下列範例會建立多個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 a 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

備註

如果當您嘗試設定此屬性時,傳入的值為 null,則會重設 Provider 屬性。 如果尚未設定值,且開發人員嘗試擷取 屬性,則傳回值為 String.Empty。 此屬性會對應至 連接字串 內的 「Provider」 索引鍵。

Provider藉由ConnectionString設定屬性) 或將 連接字串 做為參數傳遞至建構函式,直接 (屬性的值,可能會影響 實例內OleDbConnectionStringBuilder所含的索引鍵/值組集合。 例如,將 Provider 屬性設定為 「sqloledb」 ,會新增所有標準 SQL 連接字串 屬性。 如需此行為的示範,請參閱本主題中的範例。

對於某些提供者,在建構函式中OleDbConnectionStringBuilder指派 連接字串 會導致重新排列提供的索引鍵/值組順序。

適用於

另請參閱