Export (0) Print
Expand All

OdbcConnection.Database Property

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

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. The default value is an empty string ("") until the connection is opened.

Implements

IDbConnection.Database

Remarks

Initially, the Database property is set in the connection string. The Database property can be updated by using the ChangeDatabase method. If you change the current database using an SQL statement or the ChangeDatabase method, an informational message is sent and then the property is updated.

Retrieving the Database property is equivalent to calling the ODBC function SQLGetInfo with the Attribute parameter set to SQL_ATTR_CURRENT_CATALOG.

Example

[Visual Basic, C#, C++] The following example creates an OdbcConnection and changes the current database.

[Visual Basic] 
Public Sub CreateOdbcConnection()
    Dim myConnString As String = _
       "DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;"
    Dim myConnection As New OdbcConnection(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 CreateOdbcConnection() 
{
   string myConnString = "DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;";
   OdbcConnection myConnection = new OdbcConnection(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 CreateOdbcConnection()
    {
        String* myConnString = S"DRIVER= {SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;";
        OdbcConnection* myConnection = new OdbcConnection(myConnString);
        myConnection->Open();
        MessageBox::Show(String::Concat(S"ServerVersion: ", myConnection->ServerVersion, S"\nDatabase: ", myConnection->Database));
        myConnection->ChangeDatabase(S"pubs");
        MessageBox::Show(String::Concat(S"ServerVersion: ", myConnection->ServerVersion, S"\nDatabase: ", 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

OdbcConnection Class | OdbcConnection Members | System.Data.Odbc Namespace | ChangeDatabase | ConnectionString | ConnectionTimeout

Show:
© 2014 Microsoft