共用方式為


SqlConnectionStringBuilder.ContainsKey(String) 方法

定義

判斷 SqlConnectionStringBuilder 是否包含特定索引鍵。

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

參數

keyword
String

要在 SqlConnectionStringBuilder 中尋找的索引鍵。

傳回

如果 SqlConnectionStringBuilder 包含具有指定之索引鍵的項目,則為 true,否則為 false。

例外狀況

keyword 為 null (在 Visual Basic 中為 Nothing)。

範例

下列範例會 SqlConnectionStringBuilder 建立 實例、設定其部分屬性,然後藉由呼叫 ContainsKey 方法,嘗試判斷物件中是否有各種索引鍵存在。

using Microsoft.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";
    }
}

此範例會在主控台視窗中顯示以下輸出:

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

備註

因為 SqlConnectionStringBuilder 包含固定大小的索引鍵/值組集合,所以 ContainsKey 方法只會判斷特定的索引鍵名稱是否有效。

適用於