c#mysql查询语法问题

时间:2015-03-01 09:58:44

标签: c# mysql mysqli

我正在尝试将数据插入到称为游戏的表中,但是出现以下错误: 你的sql语法有错误,请查看与你的mysql服务器对应的手册

这是c#代码:

   int i = 0;
   MySqlCommand cmd = connection.CreateCommand();
   string command = string.Format("insert into game (HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR,HS,AS,HST,AST,HF,AF,HC,AC,HY,AY,HR,AR,DIV) VALUES(@HomeTeam,@AwayTeam,@FTHG,@FTAG,@FTR,@HTHG,@HTAG,@HTR,@HS,@AS,@HST,@AST,@HF,@AF,@HC,@AC,@HY,@AY,@HR,@AR,@DIV)");
   cmd.CommandText = command;
   cmd.Parameters.AddWithValue("@HomeTeam", _games[i].HomeTeam.ToString());
   cmd.Parameters.AddWithValue("@AwayTeam", _games[i].AwayTeam.ToString());
   cmd.Parameters.AddWithValue("@FTHG", Convert.ToInt32(_games[i].FTHG));
   cmd.Parameters.AddWithValue("@FTAG", Convert.ToInt32(_games[i].FTAG)); ;
   cmd.Parameters.AddWithValue("@FTR", _games[i].FTR.ToString());
   cmd.Parameters.AddWithValue("@HTHG", Convert.ToInt32(_games[i].HTHG));
   cmd.Parameters.AddWithValue("@HTAG", Convert.ToInt32(_games[i].HTAG));
   cmd.Parameters.AddWithValue("@HTR", _games[i].HTR.ToString());
   cmd.Parameters.AddWithValue("@HS", Convert.ToInt32(_games[i].HS));
   cmd.Parameters.AddWithValue("@AS", Convert.ToInt32(_games[i].AS));
   cmd.Parameters.AddWithValue("@HST", Convert.ToInt32(_games[i].HST));
   cmd.Parameters.AddWithValue("@AST", Convert.ToInt32(_games[i].AST));
   cmd.Parameters.AddWithValue("@HF", Convert.ToInt32(_games[i].HF));
   cmd.Parameters.AddWithValue("@AF", Convert.ToInt32(_games[i].AF));
   cmd.Parameters.AddWithValue("@HC", Convert.ToInt32(_games[i].HC));
   cmd.Parameters.AddWithValue("@AC", Convert.ToInt32(_games[i].AC));
   cmd.Parameters.AddWithValue("@HY", Convert.ToInt32(_games[i].HY));
   cmd.Parameters.AddWithValue("@AY", Convert.ToInt32(_games[i].AY));
   cmd.Parameters.AddWithValue("@HR", Convert.ToInt32(_games[i].HR));
   cmd.Parameters.AddWithValue("@AR", Convert.ToInt32(_games[i].AR));
   cmd.Parameters.AddWithValue("@DIV", _games[i].DIV.ToString());
   cmd.ExecuteNonQuery();

HomeTeam,AwayTeam,DIV,FTR,HTR是varchars,所有第一个都是int。 在列表中,所有字段都是字符串。

有人在这里看到任何错误?感谢。

3 个答案:

答案 0 :(得分:1)

您使用了保留字ASDIV作为列名,您需要在查询中反引号。

insert into game 
(HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR,HS,`AS`,HST,AST,.....

在此处查看保留字列表http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

答案 1 :(得分:1)

列名称中包含“AS”和“DIV”关键字。 用`

逃脱它们

试试这个:

insert into game (HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR,HS,`AS`,HST,AST,HF,AF,HC,AC,HY,AY,HR,AR,`DIV`) VALUES(@HomeTeam,@AwayTeam,@FTHG,@FTAG,@FTR,@HTHG,@HTAG,@HTR,@HS,@AS,@HST,@AST,@HF,@AF,@HC,@AC,@HY,@AY,@HR,@AR,@DIV);

答案 2 :(得分:0)

hometeam和awayteam在这里被列为班级吗? 如果是这样,请尝试将hometeam.teamname传递到数据库中。