SqlConnectionStringBuilder.ContainsKey(String) Method

Definition

确定 SqlConnectionStringBuilder 是否包含特定键。Determines whether the SqlConnectionStringBuilder contains a specific key.

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

Parameters

keyword
String

要在 SqlConnectionStringBuilder 中定位的键。The key to locate in the SqlConnectionStringBuilder.

Returns

Boolean

如果 SqlConnectionStringBuilder 包含具有指定键的元素,则为 true;否则为 falsetrue if the SqlConnectionStringBuilder contains an element that has the specified key; otherwise, false.

Exceptions

keyword 为 Null(在 Visual Basic 中为 Nothingkeyword is null (Nothing in Visual Basic)

Examples

下面的示例创建一个 SqlConnectionStringBuilder 实例,并设置其一些属性,然后尝试通过调用ContainsKey方法来确定对象中是否存在不同的键。The following example creates a SqlConnectionStringBuilder instance, sets some of its properties, and then tries to determine whether various keys exist within the object by calling the ContainsKey method.

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

该示例在控制台窗口中显示以下输出:The example displays the following output in the console window:

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

Remarks

由于 SqlConnectionStringBuilder 包含固定大小的键/值对集合,因此 ContainsKey 方法只确定特定键名是否有效。Because the SqlConnectionStringBuilder contains a fixed-size collection of key/value pairs, the ContainsKey method determines only if a particular key name is valid.

Applies to

See also