[ Team LiB ] Recipe 1.18 ChangingtheDatabaseforanOpenConnection Problem You want to change thedatabase that a connection uses without recreating the connection. Solution Use the ChangeDatabase( ) method to change thedatabasefor a connection. The sample code creates a Connection to the Northwind database using the SQL Server .NET data provider. Theconnection is changed to use the pubs database. Finally theconnection is closed. TheDatabase property of the SqlConnection object is displayed throughout the sample forthe different connection states. The C# code is shown in Example 1-12 . Example 1-12. File: ChangeDatabaseForm.cs // Namespaces, variables, and constants using System; using System.Configuration; using System.Text; using System.Data; using System.Data.SqlClient; // . . . StringBuilder result = new StringBuilder( ); // Create theconnection accessing Northwind database. SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings["Sql_ConnectString"]); result.Append("Connection String:" + Environment.NewLine); result.Append(conn.ConnectionString + Environment.NewLine + Environment.NewLine); // Openthe connection. conn.Open( ); result.Append("Connection.State: " + conn.State + Environment.NewLine); result.Append("Database: " + conn.Database + Environment.NewLine); // Change thedatabase to pubs. conn.ChangeDatabase("pubs"); result.Append("Database: " + conn.Database + Environment.NewLine); // Close the connection. conn.Close( ); result.Append("Connection.State: " + conn.State + Environment.NewLine); result.Append("Database: " + conn.Database); resultTextBox.Text = result.ToString( ); Discussion The ChangeDatabase( ) method is defined in the IDbConnection interface that represents a connection to a data source and is implemented by .NET data providers for relational databases including those for SQL Server, Oracle, and OLE DB. The ChangeDatabase( ) method is used to change the current databaseforanopen connection. It takes a single parameter that specifies the name of thedatabase to use in place of the current database. The name of thedatabase must be valid or an ArgumentException will be raised. If theconnection is not open when the method is called, an InvalidOperationException is raised. A provider-specific exception is raised if thedatabase cannot be changed for any reason. TheDatabase property of theConnection object is updated dynamically and returns the current databaseforanopenconnection or the name of a database that will be used by a closed connection when it is opened. When theConnection is closed after ChangeDatabase( ) is called, thedatabase is reset to that specified in the original connection string. [ Team LiB ] . Recipe 1.18 Changing the Database for an Open Connection Problem You want to change the database that a connection uses without recreating the connection. . Solution Use the ChangeDatabase( ) method to change the database for a connection. The sample code creates a Connection to the Northwind database using the SQL