This documentation is archived and is not being maintained.

OdbcConnection.ChangeDatabase Method

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

Changes the current database associated with an open OdbcConnection.

[Visual Basic]
Public Overridable Sub ChangeDatabase( _
   ByVal value As String _
) Implements IDbConnection.ChangeDatabase
[C#]
public virtual void ChangeDatabase(
 string value
);
[C++]
public: virtual void ChangeDatabase(
 String* value
);
[JScript]
public function ChangeDatabase(
   value : String
);

Parameters

value
The database name.

Implements

IDbConnection.ChangeDatabase

Exceptions

Exception Type Condition
ArgumentException The database name is not valid.
InvalidOperationException The connection is not open.
OdbcException Cannot change the database.

Remarks

The value parameter must contain a valid database name, and cannot contain a null value, an empty string (""), or a string with only blank characters.

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 | Database

Show: