แก้ไข

แชร์ผ่าน


SqlConnection.Database Property

Definition

Gets the name of the current database or the database to be used after a connection is opened.

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

Property Value

The name of the current database or the name of the database to be used after a connection is opened. The default value is an empty string.

Implements

Attributes

Examples

The following example creates a SqlConnection and displays some of its read-only properties.

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

Remarks

The Database property updates dynamically. If you change the current database using a Transact-SQL statement or the ChangeDatabase method, an informational message is sent and the property is updated automatically.

Applies to

See also