SqlConnectionStringBuilder.Item[String] 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定與指定鍵相關聯的值。 在 C# 中,這個屬性就是索引器。
public:
virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ keyword); void set(System::String ^ keyword, System::Object ^ value); };
public override object this[string keyword] { get; set; }
member this.Item(string) : obj with get, set
Default Public Overrides Property Item(keyword As String) As Object
參數
- keyword
- String
要取得或設定的物品鑰匙。
屬性值
與指定鍵相關聯的值。
例外狀況
我試著新增一個在可用金鑰中不存在的金鑰。
連接字串 中的無效值(具體來說,預期會輸入布林值或數字值,但未提供)。
範例
以下程式碼在主控台應用程式中,建立一個新的 SqlConnectionStringBuilder,並在其連接字串中加入鍵值對,使用 Item[] 特性。
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["Integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks";
// Overwrite the existing value for the Data Source value.
builder["Data Source"] = ".";
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
}
Module Module1
Sub Main()
Dim builder As New SqlConnectionStringBuilder
builder.Item("Data Source") = "(local)"
' Item is the default property, so
' you needn't include it in the reference.
builder("Integrated Security") = True
builder.Item("Initial Catalog") = "AdventureWorks"
' Overwrite the existing value for the Data Source value.
builder.Item("Data Source") = "."
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
Console.WriteLine("Press Enter to continue.")
Console.ReadLine()
End Sub
End Module
備註
由於 包含 SqlConnectionStringBuilder 固定大小的字典,嘗試新增字典中不存在的鍵值會拋 KeyNotFoundException出 。