When I first connect to MySQL, used dbname is DataBase1 (main DB)
Sometimes, I need to call other stored procedure named 'usp_select_query' which exists in DataBase2 (sub DB)
DataBase1,2 exist on the same server, same account.
In console, this is executed well.
call DataBase2.use_select_query; in DataBase1 // ok
c# code
DataSet ds = DbHelper.CallUSP("usp_select_query", para); // of course error
DataSet ds = DbHelper.CallUSP("DataBse2.usp_select_query", para); // error
DataSet ds = DbHelper.CallUSP("`DataBse2`.`usp_select_query`", para); // error
error Message :Procedure or function '``**DataBase2**`.`usp_select_query``' cannot be found in database '`**DataBase1**`'
public static DataSet CallUSP(string usp, params MySqlParameter[] parameters)
{
try
{
string query = usp;
if(conn.State!= ConnectionState.Open)
conn.Open();
MySqlCommand com= conn.CreateCommand();
com.CommandType = CommandType.StoredProcedure;
com.CommandText = query;
if(parameters!=null)
com.Parameters.AddRange(parameters);
MySqlDataAdapter adapter = new MySqlDataAdapter(com);
DataSet ds = new DataSet();
adapter.Fill(ds);
conn.Close();
adapter.Dispose();
com.Dispose();
return ds;
}
catch(Exception ex)
{
conn.Close();
LogE(MKSession.COMPANYINDEX,usp,ex.Message);
}
conn.Close();
return null;
}
How can I do it?
If you're using MySQL Connector/NET (aka MySql.Data
), this is a known bug in the latest version: https://bugs.mysql.com/bug.php?id=100391
You can work around it by switching to MySqlConnector instead, which has a similar API but fixes this and other bugs .
I'm just modify my method. it is may not efficency. but It is work for me.
public static DataSet CallUSP(string usp, params MySqlParameter[] parameters)
{
try
{
//add variable for dbname,usp
string mdbName = "";
string oldDbName = conn.Database;
string[] lines = usp.Split(new string[] { "." }, StringSplitOptions.None);
//set the value
if (lines.Length > 1)
{
mdbName = lines[0];
usp = lines[1];
}
string query = usp;
if(conn.State!= ConnectionState.Open)
conn.Open();
//change database
if (mdbName != "")
conn.ChangeDatabase(mdbName);
MySqlCommand com= conn.CreateCommand();
com.CommandType = CommandType.StoredProcedure;
com.CommandText = usp;
if(parameters!=null)
com.Parameters.AddRange(parameters);
MySqlDataAdapter adapter = new MySqlDataAdapter(com);
DataSet ds = new DataSet();
adapter.Fill(ds);
//reset orgin databse
if (mdbName != "")
conn.ChangeDatabase(oldDbName);
conn.Close();
adapter.Dispose();
com.Dispose();
return ds;
}
catch(Exception ex)
{
conn.Close();
LogE(MKSession.COMPANYINDEX,usp,ex.Message);
}
conn.Close();
return null;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.