Export (0) Print
Expand All

OleDbConnection.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 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 an OleDbConnection and displays some of its read-only properties.

[Visual Basic] 
Public Sub CreateOleDbConnection()
    Dim myConnString As String = _
       "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"
    Dim myConnection As New OleDbConnection(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 CreateOleDbConnection() 
{
   string myConnString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;";
   OleDbConnection myConnection = new OleDbConnection(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 CreateOleDbConnection() 
 {
    String* myConnString = S"Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;";
    OleDbConnection* myConnection = new OleDbConnection(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

See Also

OleDbConnection Class | OleDbConnection Members | System.Data.OleDb Namespace | ChangeDatabase | ConnectionString | ConnectionTimeout | DataSource

Show:
© 2014 Microsoft