简体   繁体   English

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

[英]Ignoring the @ symbol when running a MySQL command from C#

I have the following MySQL command that populates a table with the last 6 matches played by soccer teams: 我有以下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;

I want to run this from within a C# program, but when I do the query fails as the @ symbol is taken as a query parameter, rather than being treated in the way that MySQL intended (a MySQL variable?). 我想在C#程序中运行它,但是当我这样做时查询失败,因为@符号被当作查询参数,而不是以MySQL预期的方式(MySQL变量?)来对待。

Within my C# program I am just declaring a string to hold the query and adding it to a MySqlCommand: 在我的C#程序中,我只是声明一个字符串来保存查询并将其添加到MySqlCommand中:

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

Is there a way to get C# to ignore the @ symbol so that @teamCounter and @prevHome are not treated as query parameters? 有没有一种方法可以使C#忽略@符号,以便将@teamCounter和@prevHome不视为查询参数?

正确的做法是使它成为数据库中的存储过程,并让您的C#运行该存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM