c# - 将数据插入名为 game 的表

标签 c# mysql

我正在尝试将数据插入到名为 game 的表中,但出现以下错误: 您的 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。 在列表中,所有字段都是字符串。

有人看到这里有什么错误吗?

最佳答案

您在列名称处有“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);

关于c# - 将数据插入名为 game 的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28792214/

相关文章:

c# - .NET 货币格式化程序 : can I specify the use of banker's rounding?

c# - 为什么我需要使用 Activator CreateInstance?

php - 我的 MySQL 数据库中的正则表达式不工作

MYSQL同步/数据从多个数据库插入到单个目标数据库

c#正则表达式问题

c# - 如何在 ASP.NET MVC3 中表示不同的产品级别和功能访问?

c# - 数据库查询中的存储过程与代码

php - Symfony2 将选定的日期从表单保存到数据库作为 DateTime

mysql - 用通配符替换字符串

mysql - 如何将时间戳设置回 0000-00-00 00 :00:00?