SqlConnection.Database 属性

定义

获取当前数据库的名称或打开连接后要使用的数据库的名称。

public:
 virtual property System::String ^ Database { System::String ^ get(); };
public:
 property System::String ^ Database { System::String ^ get(); };
public override string Database { get; }
[System.Data.DataSysDescription("SqlConnection_Database")]
public string Database { get; }
member this.Database : string
[<System.Data.DataSysDescription("SqlConnection_Database")>]
member this.Database : string
Public Overrides ReadOnly Property Database As String
Public ReadOnly Property Database As String

属性值

当前数据库的名称或打开连接后要使用的数据库的名称。 默认值为一个空字符串。

实现

属性

示例

以下示例创建 并 SqlConnection 显示其一些只读属性。

private static void ChangeSqlDatabase(string connectionString)
{
    // Assumes connectionString represents a valid connection string
    // to the AdventureWorks sample database.
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("Database: {0}", connection.Database);

        connection.ChangeDatabase("Northwind");
        Console.WriteLine("Database: {0}", connection.Database);
    }
}
Private Sub ChangeSqlDatabase(ByVal connectionString As String)
    ' Assumes connectionString represents a valid connection string
    ' to the AdventureWorks sample database.
    Using connection As New SqlConnection(connectionString)

        connection.Open()
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion)
        Console.WriteLine("Database: {0}", connection.Database)

        connection.ChangeDatabase("Northwind")
        Console.WriteLine("Database: {0}", connection.Database)
    End Using
End Sub

注解

属性 Database 动态更新。 如果使用 Transact-SQL 语句或 ChangeDatabase 方法更改当前数据库,则会发送信息性消息并自动更新 属性。

适用于

另请参阅