SqlConnectionStringBuilder.ContainsKey(String) Metodo

Definizione

Determina se l'oggetto SqlConnectionStringBuilder contiene una chiave specifica.

public:
 override bool ContainsKey(System::String ^ keyword);
public override bool ContainsKey (string keyword);
override this.ContainsKey : string -> bool
Public Overrides Function ContainsKey (keyword As String) As Boolean

Parametri

keyword
String

Chiave da individuare in SqlConnectionStringBuilder.

Restituisce

true se la classe SqlConnectionStringBuilder contiene un elemento con la chiave specificata; in caso contrario, false.

Eccezioni

keyword è Null (Nothing in Visual Basic).

Esempio

L'esempio seguente crea un'istanza SqlConnectionStringBuilder , imposta alcune delle relative proprietà e quindi tenta di determinare se esistono diverse chiavi all'interno dell'oggetto chiamando il metodo ContainsKey .

using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        SqlConnectionStringBuilder builder =
            new SqlConnectionStringBuilder(GetConnectionString());
        Console.WriteLine("Connection string = " + builder.ConnectionString);

        // Keys you have provided return true.
        Console.WriteLine(builder.ContainsKey("Server"));

        // Comparison is case insensitive, and synonyms
        // are automatically converted to their "well-known"
        // names.
        Console.WriteLine(builder.ContainsKey("Database"));

        // Keys that are valid but have not been set return true.
        Console.WriteLine(builder.ContainsKey("Max Pool Size"));

        // Keys that do not exist return false.
        Console.WriteLine(builder.ContainsKey("MyKey"));

        Console.WriteLine("Press Enter to continue.");
        Console.ReadLine();
    }

    private static string GetConnectionString()
    {
        // To avoid storing the connection string in your code,
        // you can retrieve it from a configuration file.
        return "Server=(local);Integrated Security=SSPI;" +
            "Initial Catalog=AdventureWorks";
    }
}
Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Dim builder As _
         New SqlConnectionStringBuilder(GetConnectionString())
        Console.WriteLine("Connection string = " & builder.ConnectionString)

        ' Keys you have provided return true.
        Console.WriteLine(builder.ContainsKey("Server"))

        ' Comparison is case insensitive, and synonyms
        ' are automatically converted to their "well-known"
        ' names.
        Console.WriteLine(builder.ContainsKey("Database"))

        ' Keys that are valid but have not been set return true.
        Console.WriteLine(builder.ContainsKey("Max Pool Size"))

        ' Keys that do not exist return false.
        Console.WriteLine(builder.ContainsKey("MyKey"))

        Console.WriteLine("Press Enter to continue.")
        Console.ReadLine()
    End Sub

    Private Function GetConnectionString() As String
        ' To avoid storing the connection string in your code,
        ' you can retrieve it from a configuration file. 
        Return "Server=(local);Integrated Security=SSPI;" & _
          "Initial Catalog=AdventureWorks"
    End Function
End Module

Nell'esempio viene visualizzato il seguente output nella finestra della console:

Connection string = Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
True
True
True
False

Commenti

Poiché SqlConnectionStringBuilder contiene una raccolta a dimensione fissa di coppie chiave/valore, il metodo ContainsKey determina solo se è valido un nome della chiave particolare.

Si applica a

Vedi anche