简体   繁体   中英

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:

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?).

Within my C# program I am just declaring a string to hold the query and adding it to a 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#运行该存储过程。

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.

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