为打开的 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
);参数
database 
要代替当前数据库进行使用的数据库的名称。 

解决方案 »

  1.   

    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();
    }
      

  2.   

    以上两个都不好用。试过,问题如下public void InsertRow(string myConnectionString) 
     {
        if(myConnectionString == "") 
        {
           myConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
        }
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        string myInsertQuery = "Select * from Customers";
        SqlCommand myCommand = new SqlCommand(myInsertQuery);
        myCommand.Connection = myConnection;
        myConnection.Open();
        myCommand.ExecuteNonQuery();    //执行成功
        myCommand.Connection.Close();
        
        myConnection.Open();
        myConnection.ChangeDataBase("Pubs");
        string myInsertQuery = "Select * from sales";
        SqlCommand myCommand = new SqlCommand(myInsertQuery);
        myCommand.Connection = myConnection;
        myCommand.ExecuteNonQuery();    //执行失败:不能发现对象“sales”
        myCommand.Connection.Close(); }
      

  3.   

    myCommand.Connection.Close();myConnection.Open();
    myConnection.ChangeDataBase("Pubs");
    ===================================>myConnection.ChangeDataBase("Pubs");
    你不要关了再开,直接开着换。