OleDbConnectionStringBuilder.Keys Właściwość

Definicja

Pobiera element ICollection zawierający klucze w elemecie OleDbConnectionStringBuilder.

C#
public override System.Collections.ICollection Keys { get; }

Wartość właściwości

Element ICollection zawierający klucze w elemecie OleDbConnectionStringBuilder.

Przykłady

Poniższy przykład aplikacji konsolowej tworzy nowy OleDbConnectionStringBuilderelement . Kod jest zapętlany Keys przez ICollection właściwość wyświetlającą pary klucz/wartość.

Uwaga

Ten przykład zawiera hasło do zademonstrowania OleDbConnectionStringBuilder sposobu działania z parametrami połączenia. W aplikacjach zalecamy użycie uwierzytelniania systemu Windows. Jeśli musisz użyć hasła, nie dołączaj hasła zakodowanego w aplikacji.

C#
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());
        }
    }
}

Uwagi

Kolejność wartości w obiekcie ICollection jest nieokreślona, ale jest taka sama jak skojarzona wartość zwrócona ICollection przez Values właściwość.

Dotyczy

Produkt Wersje
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Zobacz też