SqlConnectionStringBuilder.ContainsKey(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷 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 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
此範例會在主控台視窗中顯示以下輸出:
Connection string = Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
True
True
True
False
備註
因為 SqlConnectionStringBuilder 包含固定大小的索引鍵/值組集合,所以 ContainsKey 方法只會判斷特定的索引鍵名稱是否有效。
適用於
另請參閱
- ADO.NET 中的連接字串
- ADO.NET 概觀 \(部分機器翻譯\)