DbConnectionStringBuilder.Keys 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 ICollection 中包含索引鍵的 DbConnectionStringBuilder。
public:
virtual property System::Collections::ICollection ^ Keys { System::Collections::ICollection ^ get(); };
public virtual System.Collections.ICollection Keys { get; }
[System.ComponentModel.Browsable(false)]
public virtual System.Collections.ICollection Keys { get; }
member this.Keys : System.Collections.ICollection
[<System.ComponentModel.Browsable(false)>]
member this.Keys : System.Collections.ICollection
Public Overridable ReadOnly Property Keys As ICollection
屬性值
ICollection 中包含索引鍵的 DbConnectionStringBuilder。
實作
- 屬性
範例
下列主控台應用程式範例會建立新的 DbConnectionStringBuilder,並新增一些金鑰。 程式代碼會循環顯示 ICollection 索引鍵/值組的屬性所 Keys 傳回的 ,然後加入新的索引鍵。 由於屬性會 Keys 傳回動態 ICollection,因此第二個循環會顯示所有索引鍵/值組,包括最新的專案。
static void Main()
{
DbConnectionStringBuilder builder = new
DbConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["Integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks";
// Obtain reference to the collection of keys.
ICollection keys = builder.Keys;
Console.WriteLine("Keys before adding TimeOut:");
foreach (string key in keys)
Console.WriteLine("{0}={1}", key, builder[key]);
// Add a new item to the collection.
builder["Timeout"] = 300;
Console.WriteLine();
Console.WriteLine("Keys after adding TimeOut:");
// Because the Keys property is dynamically updated,
// the following loop includes the Timeout key.
foreach (string key in keys)
Console.WriteLine("{0}={1}", key, builder[key]);
Console.WriteLine();
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
Sub Main()
Dim builder As New DbConnectionStringBuilder
builder("Data Source") = "(local)"
builder("integrated security") = True
builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"
' Obtain reference to the collection of keys.
Dim keys As ICollection = builder.Keys
Console.WriteLine("Keys before adding TimeOut:")
For Each key As String In keys
Console.WriteLine("{0}={1}", key, builder(key))
Next
' Add a new item to the collection.
builder("Timeout") = 300
Console.WriteLine()
Console.WriteLine("Keys after adding TimeOut:")
' Because the Keys property is dynamically updated,
' the following loop includes the Timeout key.
For Each key As String In keys
Console.WriteLine("{0}={1}", key, builder(key))
Next
Console.WriteLine()
Console.WriteLine("Press Enter to continue.")
Console.ReadLine()
End Sub
備註
雖然 ICollection 中值的順序並未指定,但此順序會與 ICollection 屬性傳回之 Values 中關聯值的順序相同。
傳 ICollection 回的 不是靜態複本, ICollection 而是會參考原始 DbConnectionStringBuilder中的索引鍵。 因此, 變更 DbConnectionStringBuilder 會反映在中 ICollection。
適用於
另請參閱
- ADO.NET 中的連接字串
- ADO.NET 概觀 \(部分機器翻譯\)