ChangeDatabase Method

MySQL Connector/Net

Changes the current database for an open MySqlConnection.

Namespace:  MySql.Data.MySqlClient
Assembly:  MySql.Data (in MySql.Data.dll) Version: 6.2.2.0

Syntax

C#
public override void ChangeDatabase(
	string databaseName
)
Visual Basic (Declaration)
Public Overrides Sub ChangeDatabase ( _
	databaseName As String _
)
Visual C++
public:
virtual void ChangeDatabase(
	String^ databaseName
) override

Parameters

databaseName
Type: System..::.String
The name of the database to use.

Implements

IDbConnection..::.ChangeDatabase(String)

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 MySQL, and you close the connection, it is returned to the connection pool. The next time the connection is retrieved from the pool, the reset connection request executes before the user performs any operations.

Examples

The following example creates a MySqlConnection and displays some of its read-only properties.
CopyVB.NET
Public Sub CreateMySqlConnection()
Dim myConnString As String = _
"Persist Security Info=False;database=test;server=localhost;user id=joeuser;pwd=pass"
Dim myConnection As New MySqlConnection( myConnString )
myConnection.Open()
MessageBox.Show( "Server Version: " + myConnection.ServerVersion _
+ ControlChars.NewLine + "Database: " + myConnection.Database )
myConnection.ChangeDatabase( "test2" )
MessageBox.Show( "ServerVersion: " + myConnection.ServerVersion _
+ ControlChars.NewLine + "Database: " + myConnection.Database )
myConnection.Close()
End Sub
CopyC#
public void CreateMySqlConnection()
{
string myConnString =
"Persist Security Info=False;database=test;server=localhost;user id=joeuser;pwd=pass";
MySqlConnection myConnection = new MySqlConnection( myConnString );
myConnection.Open();
MessageBox.Show( "Server Version: " + myConnection.ServerVersion
+ "\nDatabase: " + myConnection.Database );
myConnection.ChangeDatabase( "test2" );
MessageBox.Show( "ServerVersion: " + myConnection.ServerVersion
+ "\nDatabase: " + myConnection.Database );
myConnection.Close();
}

Exceptions

Exception Condition
System..::.ArgumentException The database name is not valid.
System..::.InvalidOperationException The connection is not open.
MySql.Data.MySqlClient..::.MySqlException Cannot change the database.

See Also