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

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

适用于

另请参阅