SqlConnection.Database Property


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

public override string Database { get; }
member this.Database : string
Public Overrides 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.


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

using Microsoft.Data.SqlClient;

class Program1
    static void Main()
        string s = GetConnectionString();


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

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

    static private string GetConnectionString()
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file, using the 
        // System.Configuration.ConfigurationSettings.AppSettings property 
        return "Data Source=(local);Initial Catalog=AdventureWorks;"
            + "Integrated Security=SSPI;";


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