This documentation is archived and is not being maintained.

SqlConnection.ChangeDatabase Method

Changes the current database for an open SqlConnection.

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

Parameters

database
The name of the database to use in place of the current database.

Implements

IDbConnection.ChangeDatabase

Exceptions

Exception Type Condition
ArgumentException The database name is not valid.
InvalidOperationException The connection is not open.
SqlException 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.

When you are using connection pooling against SQL Server 2000, and you close the connection, it is returned to the connection pool. The next time the connection is retrieved from the pool used, the reset connection request piggybacks on the first round trip to the server and executes before the user performs any operations. From the time you return a connection to the connection pool after calling the ChangeDatabase method, until the time the connection is retrieved, the connection remains active on the database. This will prevent you from dropping the database because the connection is still active.

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

Show: