SqlConnection.Database Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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 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.
Examples
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();
ChangeSqlDatabase(s);
Console.ReadLine();
}
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);
}
}
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;";
}
}
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.