DbConnectionStringBuilder.ConnectionString 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定與 DbConnectionStringBuilder 相關聯的連接字串。
public:
property System::String ^ ConnectionString { System::String ^ get(); void set(System::String ^ value); };
public string ConnectionString { get; set; }
member this.ConnectionString : string with get, set
Public Property ConnectionString As String
屬性值
從包含於 DbConnectionStringBuilder 的索引鍵/值組所建立的目前連接字串。 預設值為空字串。
例外狀況
已提供無效的連接字串引數。
範例
下列範例示範 屬性的 ConnectionString 可能行為。 範例:
藉由將索引鍵/值組一次新增至空DbConnectionStringBuilder的 ,以建立 連接字串。
將完整的 連接字串 指派給 ConnectionString 實例的 DbConnectionStringBuilder 屬性,並修改字串內的單一索引鍵/值組。
將任意的索引鍵/值組指派給ConnectionString屬性 (,也就是不是任何遠端的字串,例如 連接字串) ,並修改其中一個值。
將無效 連接字串 指派給 ConnectionString 屬性,示範擲回的例外狀況。
注意
這個範例包含了密碼,可示範 DbConnectionStringBuilder 如何搭配連接字串使用。 我們建議在您的應用程式中使用 Windows 驗證。 如果您必須使用密碼,請勿在您的應用程式中包含硬式編碼的密碼。
static void Main()
{
// Create a new DbConnctionStringBuilder, and add items
// to the internal collection of key/value pairs.
DbConnectionStringBuilder builder = new
DbConnectionStringBuilder();
builder.Add("Data Source", @"c:\MyData\MyDb.mdb");
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
builder.Add("Jet OLEDB:Database Password", "********");
builder.Add("Jet OLEDB:System Database",
@"c:\MyData\Workgroup.mdb");
// Set up row-level locking.
builder.Add("Jet OLEDB:Database Locking Mode", 1);
// Display the contents of the connection string, which
// will now contain all the key/value pairs delimited with
// semicolons.
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
// Clear the DbConnectionStringBuilder, and assign a complete
// connection string to it, to demonstrate how
// the class parses connection strings.
builder.Clear();
builder.ConnectionString =
"Data Source=(local);Initial Catalog=AdventureWorks;"
+ "Integrated Security=SSPI";
// The DbConnectionStringBuilder class has parsed the contents,
// so you can work with any individual key/value pair.
builder["Data Source"] = ".";
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
// Because the DbConnectionStringBuilder class doesn't
// validate its key/value pairs, you can use this class
// to store any semicolon-delimited list. The following
// snippet places an arbitrary string into the ConnectionString
// property, changes one of the values, and then displays the
// resulting string.
builder.Clear();
builder.ConnectionString =
"Value1=10;Value2=20;Value3=30;Value4=40";
builder["Value2"] = 25;
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
builder.Clear();
try
{
// Assigning an invalid connection string
// throws an ArgumentException.
builder.ConnectionString = "xxx";
}
catch (ArgumentException)
{
Console.WriteLine("Invalid connection string.");
}
Console.WriteLine();
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
Sub Main()
' Create a new DbConnctionStringBuilder, and add items
' to the internal collection of key/value pairs.
Dim builder As New DbConnectionStringBuilder()
builder.Add("Data Source", "c:\MyData\MyDb.mdb")
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
builder.Add("Jet OLEDB:Database Password", "*******")
builder.Add("Jet OLEDB:System Database", _
"c:\MyData\Workgroup.mdb")
' Set up row-level locking.
builder.Add("Jet OLEDB:Database Locking Mode", 1)
' Display the contents of the connection string, which
' will now contain all the key/value pairs delimited with
' semicolons.
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
' Clear the DbConnectionStringBuilder, and assign a complete
' connection string to it, to demonstrate how
' the class parses connection strings.
builder.Clear()
builder.ConnectionString = _
"Data Source=(local);Initial Catalog=AdventureWorks;" & _
"Integrated Security=SSPI"
' The DbConnectionStringBuilder class has parsed the contents,
' so you can work with any individual key/value pair.
builder("Data Source") = "."
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
' Because the DbConnectionStringBuilder class doesn't
' validate its key/value pairs, you can use this class
' to store any semicolon-delimited list. The following
' snippet places an arbitrary string into the ConnectionString
' property, changes one of the values, and then displays the
' resulting string.
builder.Clear()
builder.ConnectionString = _
"Value1=10;Value2=20;Value3=30;Value4=40"
builder("Value2") = 25
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
builder.Clear()
Try
' Assigning an invalid connection string
' throws an ArgumentException.
builder.ConnectionString = "xxx"
Catch ex As ArgumentException
Console.WriteLine("Invalid connection string.")
End Try
Console.WriteLine()
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Sub
備註
這個屬性會傳回以分號分隔的索引鍵/值組清單,這些索引鍵/值組儲存在 所 DbConnectionStringBuilder維護的集合內。 每個配對都包含索引鍵和值,並以等號分隔。 下列範例說明典型的 連接字串。
"Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=AdventureWorks;Data Source=(local)"
數據提供者可能會預期每個 連接字串 屬性都有特定的索引鍵和值。 ,這些值會個別記載。 類別DbConnectionStringBuilder不會驗證與其 連接字串 相關聯的索引鍵/值組,雖然繼承自它的類別可以。
類別 ConnectionString 的 DbConnectionStringBuilder 屬性通常可作為建立和剖析以等號分隔之索引鍵/值組之分號分隔清單的機制。 它不提供連接字串特定的驗證或其他支援。 如果您將專案新增至 DbConnectionStringBuilder 集合,屬性 ConnectionString 將會反映變更。 如果您將值指派給 ConnectionString 屬性, DbConnectionStringBuilder 則會嘗試使用分號和等號分隔符來剖析值。
適用於
另請參閱
- ADO.NET 中的連接字串
- ADO.NET 概觀 \(部分機器翻譯\)