共用方式為


OleDbConnectionStringBuilder.Keys 屬性

定義

取得 ICollection 中包含索引鍵的 OleDbConnectionStringBuilder

public:
 virtual property System::Collections::ICollection ^ Keys { System::Collections::ICollection ^ get(); };
public override System.Collections.ICollection Keys { get; }
member this.Keys : System.Collections.ICollection
Public Overrides ReadOnly Property Keys As ICollection

屬性值

ICollection 中包含索引鍵的 OleDbConnectionStringBuilder

範例

下列主控台應用程式範例會建立新的 OleDbConnectionStringBuilder。 此程式碼會針對 ICollection 屬性傳回的 Keys 執行迴圈,以顯示索引鍵/值組。

注意

這個範例包含了密碼,可示範 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

備註

雖然 ICollection 中值的順序並未指定,但此順序會與 ICollection 屬性傳回之 Values 中關聯值的順序相同。

適用於

另請參閱