从C#运行MySQL命令时忽略@符号

时间:2013-03-31 16:55:30

标签: c# mysql

我有以下MySQL命令填充表格,其中包含足球队最后6场比赛:

insert into lastxgames
select team, matchdate, hometeam, awayteam, fthg, ftag
from (
  select
    team, soccerdata.matchdate, hometeam, awayteam, fthg, ftag,
    @teamCounter:=IF(@prevHome=team,@teamCounter+1,1) teamCounter,
    @prevHome:=team
  from soccerdata
    join (
      select distinct matchdate, hometeam team
      from soccerdata
      union 
      select distinct matchdate, awayteam
      from soccerdata
    ) allgames on soccerdata.matchdate = allgames.matchdate
      and (soccerdata.hometeam = allgames.team or soccerdata.awayteam = allgames.team)
    join (select @teamCounter:=0) t
  order by team, soccerdata.matchdate desc
  ) t 
where teamCounter <= 6
order by team, matchdate desc;

我想在C#程序中运行它,但是当我执行查询失败时,@符号被视为查询参数,而不是像MySQL所期望的那样处理(MySQL变量?)。 / p>

在我的C#程序中,我只是声明一个字符串来保存查询并将其添加到MySqlCommand:

string insertCommand = @"insert into lastxgames...";
using (MySqlCommand cmdInsert = new MySqlCommand(insertCommand, dcConnection))
{
  cmdInsert.ExecuteNonQuery();
}

有没有办法让C#忽略@符号,以免@teamCounter和@prevHome被视为查询参数?

1 个答案:

答案 0 :(得分:0)

正确的做法是将其作为数据库中的存储过程,让C#运行该sproc。