SqlConnectionStringBuilder.InitialCatalog 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定與連接相關聯的資料庫名稱。
public:
property System::String ^ InitialCatalog { System::String ^ get(); void set(System::String ^ value); };
public string InitialCatalog { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Data.SqlClient.SqlConnectionStringBuilder+SqlInitialCatalogConverter))]
public string InitialCatalog { get; set; }
member this.InitialCatalog : string with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Data.SqlClient.SqlConnectionStringBuilder+SqlInitialCatalogConverter))>]
member this.InitialCatalog : string with get, set
Public Property InitialCatalog As String
屬性值
InitialCatalog 屬性的值,如未提供任何值,則為 String.Empty
。
- 屬性
例外狀況
若要將值設定為 null,請使用 Value。
範例
下列範例會建立簡單的連接字串,然後使用 SqlConnectionStringBuilder 類別,將資料庫的名稱加入至連接字串。 此程式碼會顯示 InitialCatalog 屬性的內容,其目的只是為了確認此類別能夠從同義資料表 ("Database") 轉換成適當的屬性值。
using System.Data.SqlClient;
class Program
{
static void Main()
{
try
{
string connectString = "Data Source=(local);" +
"Integrated Security=true";
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(connectString);
Console.WriteLine("Original: " + builder.ConnectionString);
// Normally, you could simply set the InitialCatalog
// property of the SqlConnectionStringBuilder object. This
// example uses the default Item property (the C# indexer)
// and the "Database" string, simply to demonstrate that
// setting the value in this way results in the same
// connection string:
builder["Database"] = "AdventureWorks";
Console.WriteLine("builder.InitialCatalog = "
+ builder.InitialCatalog);
Console.WriteLine("Modified: " + builder.ConnectionString);
using (SqlConnection connection =
new SqlConnection(builder.ConnectionString))
{
connection.Open();
// Now use the open connection.
Console.WriteLine("Database = " + connection.Database);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
}
Imports System.Data.SqlClient
Module Module1
Sub Main()
Try
Dim connectString As String = _
"Data Source=(local);" & _
"Integrated Security=True"
Dim builder As New SqlConnectionStringBuilder(connectString)
Console.WriteLine("Original: " & builder.ConnectionString)
' Normally, you could simply set the InitialCatalog
' property of the SqlConnectionStringBuilder object. This
' example uses the default Item property (the C# indexer)
' and the "Database" string, simply to demonstrate that
' setting the value in this way results in the same
' connection string.
builder("Database") = "AdventureWorks"
Console.WriteLine("builder.InitialCatalog = " _
& builder.InitialCatalog)
Console.WriteLine("Modified: " & builder.ConnectionString)
Using connection As New SqlConnection(builder.ConnectionString)
connection.Open()
' Now use the open connection.
Console.WriteLine("Database = " & connection.Database)
End Using
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.WriteLine("Press any key to finish.")
Console.ReadLine()
End Sub
End Module
備註
此屬性會對應至連接字串內的 "Initial Catalog" 和 "database" 索引鍵。
適用於
另請參閱
- ADO.NET 中的連接字串
- ADO.NET 概觀 \(部分機器翻譯\)