This documentation is archived and is not being maintained.

OleDbConnection.ChangeDatabase Method

Changes the current database for an open OleDbConnection.

[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.
OleDbException Cannot change the database.

Remarks

The value supplied in the database parameter must be a valid database name. The database parameter 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 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 | Database

Show: