Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SqlConnection.Database Property

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

[Visual Basic]
Public Overridable ReadOnly Property Database As String  Implements _
   IDbConnection.Database
[C#]
public virtual string Database {get;}
[C++]
public: __property virtual String* get_Database();
[JScript]
public function get Database() : 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

IDbConnection.Database

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.

Example

[Visual Basic, C#, C++] The following example creates a SqlConnection and displays some of its read-only properties.

[Visual Basic] 
Public Sub CreateSqlConnection()
    Dim myConnString As String = _
       "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;data source=mySQLServer"
    Dim myConnection As New SqlConnection(myConnString)
    myConnection.Open()
    MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
       + ControlChars.NewLine + "Database: " + myConnection.Database)
    myConnection.ChangeDatabase("pubs")
    MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
       + ControlChars.NewLine + "Database: " + myConnection.Database)
    myConnection.Close()
End Sub

[C#] 
public void CreateSqlConnection() 
{
   string myConnString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;data source=mySQLServer";
   SqlConnection myConnection = new SqlConnection(myConnString);
   myConnection.Open();
   MessageBox.Show("ServerVersion: " + myConnection.ServerVersion
      + "\nDatabase: " + myConnection.Database);
   myConnection.ChangeDatabase("pubs");
   MessageBox.Show("ServerVersion: " + myConnection.ServerVersion
      + "\nDatabase: " + myConnection.Database);
   myConnection.Close();
}

[C++] 
public:
 void CreateSqlConnection() 
 {
    String* myConnString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;data source=mySQLServer";
    SqlConnection* myConnection = new SqlConnection(myConnString);
    myConnection->Open();
    MessageBox::Show(String::Format( S"ServerVersion: {0}\nDatabase: {1}", myConnection->ServerVersion, myConnection->Database ));
    myConnection->ChangeDatabase(S"pubs");
    MessageBox::Show(String::Format( S"ServerVersion: {0}\nDatabase: {1}", myConnection->ServerVersion, myConnection->Database ));
    myConnection->Close();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

SqlConnection Class | SqlConnection Members | System.Data.SqlClient Namespace | ChangeDatabase | ConnectionString | ConnectionTimeout | DataSource

Show:
© 2015 Microsoft