所以我有一个真正非常大的预编译MySQL查询集。这个查询在我的C#应用程序之外执行得非常好,但是当我尝试在我的C#应用程序中执行它时,我遇到了一个问题。请注意我不能修改查询本身,也不能在代码中重新编译它,我需要它按原样执行。
查询本身由遵循以下模式的众多不同查询组成:
DELETE FROM stores WHERE region_id = 1;
INSERT INTO stores (region_id, name, score, type) VALUES (17, '1', 0, 'Profit'),(17, '2', 0, 'Profit'),(17, '3', 0, 'Profit'),(17, '4', 0, 'Profit'),(17, '5', 0, 'Profit'),(17, '6', 0, 'NonProfit'),(17, '7', 0, 'NonProfit'),(17, '8', 0, 'NonProfit'),(17, '9', 0, 'NonProfit'),(17, '10', 0, 'NonProfit');
SELECT id INTO @store_id0 FROM stores WHERE region_id=17 AND name = '1' AND type = 'Profit';
INSERT INTO store_merch(store_id, item_id, stock) VALUES (@store_id0, 1360, 0),(@store_id0, 440, 5);
当我在类似navicat(数据库管理工具)的程序上执行它时,这可以正常工作,但是在我的C#程序中,它会产生以下异常:
参数' @ store_id0'必须定义。
这是我的查询执行功能...
public bool ExecuteQuery(
string raw_query)
{
if (this.Open() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(raw_query, connection);
try
{
int changed_rows = cmd.ExecuteNonQuery();
if (changed_rows > 0)
{
this.Close();
return true;
}
else
{
Logger.GetInstance().Error("MySQLConnection::ExecuteQuery", "Zero records changed for in raw query.");
}
}
catch (MySqlException e)
{
Logger.GetInstance().Error("MySQLConnection::ExecuteQuery", e.Message);
}
this.Close();
}
return false;
}
任何人都知道问题是什么?
答案 0 :(得分:-1)
//您可以使用StringBuilder并编写多个查询
StringBuilder strb = new StringBuilder(); strb.Append(" SELECT IncentiveCode as Code,IncentiveDescription as PoliceyName,");
strb.Append(" IncPeriodCode as IncetvePeriodCode, PolP.PoliceyPeriodDesc as IncetivePeriodName,");
strb.Append(" EffectedDate ,EndDate ,pol.SuppCode as SuppCode ,comp.SuppName as SuppName,pol.IsActive from IncentivePolicey pol");
strb.Append(" LEFT outer join IncentivePoliceyPeriod PolP");
strb.Append(" ON pol.IncPeriodCode = PolP.PoliceyPeriodCode");
strb.Append(" LEFT OUTER JOIN Inventory_Suppliers comp ON pol.SuppCode = comp.SuppCode");
sqlcommandText = strb.toString();
答案 1 :(得分:-2)
对于模式为@name
的每个变量,您必须创建一个参数并提供其值:
cmd.Parameters.AddWithValue("@store_id0", 123);