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
要获取或设置的项的键。
属性值
与指定键关联的值。
例外
keyword是空引用(Visual Basic 中的 Nothing)。
尝试添加可用密钥中不存在的密钥。
连接字符串中的值无效(具体而言,预期是布尔值或数值,但未提供)。
示例
在控制台应用程序中,以下代码使用 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。