共用方式為


SqlConnectionStringBuilder.InitialCatalog 屬性

定義

取得或設定與連接相關聯的資料庫名稱。

public:
 property System::String ^ InitialCatalog { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter(typeof(Microsoft.Data.SqlClient.SqlConnectionStringBuilder+SqlInitialCatalogConverter))]
public string InitialCatalog { get; set; }
public string InitialCatalog { get; set; }
[<System.ComponentModel.TypeConverter(typeof(Microsoft.Data.SqlClient.SqlConnectionStringBuilder+SqlInitialCatalogConverter))>]
member this.InitialCatalog : string with get, set
member this.InitialCatalog : string with get, set
Public Property InitialCatalog As String

屬性值

InitialCatalog 屬性的值,如未提供任何值,則為 String.Empty

屬性

例外狀況

若要將值設定為 null,請使用 Value

範例

下列範例會建立簡單的連接字串,然後使用 SqlConnectionStringBuilder 類別,將資料庫的名稱加入至連接字串。 此程式碼會顯示 InitialCatalog 屬性的內容,其目的只是為了確認此類別能夠從同義資料表 ("Database") 轉換成適當的屬性值。

using Microsoft.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();
    }
}

備註

此屬性會對應至連接字串內的 "Initial Catalog" 和 "database" 索引鍵。

資料庫名稱可以是 128 個字元或更少。

適用於