试图插入数据表的数组值时出现语法错误

本文关键字:语法 错误 数组 插入 数据表 | 更新日期: 2023-09-27 18:09:41

当我试图在mysql数据库中插入名为'table'的数据表数组值时,我有一个SQL语法错误。

ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.27-community-nt]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right.

Datatable = table;

代码如下:

 for (int x = 0; x < table.Rows.Count; x++)
   {
      conn.Open();
      OdbcCommand command = new OdbcCommand("INSERT INTO jevslimporttemp(JevSLImportTempCode, JevSLImportTempDescription, JevSLImportTempAmount)VALUES('" + table.Rows[x][0].ToString() + "','" + table.Rows[x][1].ToString() + "','" + Convert.ToDouble(table.Rows[x][2].ToString()) + "');", conn);
      command.ExecuteNonQuery();
      conn.Close();
   }

试图插入数据表的数组值时出现语法错误

您的变量中很可能有干扰sql的值,如单引号或问号。您应该参数化您的查询。

OdbcCommand command = new OdbcCommand("INSERT INTO jevslimporttemp(JevSLImportTempCode, JevSLImportTempDescription, JevSLImportTempAmount)VALUES(?, ?, ?);", conn);
command.Parameters.AddWithValue("JevSLImportTempCode", table.Rows[x][1].ToString());
command.Parameters.AddWithValue("JevSLImportTempDescription", table.Rows[x][1].ToString());
command.Parameters.AddWithValue("JevSLImportTempAmount", Convert.ToDouble(table.Rows[x][2].ToString()));

看起来您的三个字段都不是字符串类型。从字段名来看,我猜第三个字段是数字。第三个字段不加引号:

OdbcCommand command = new OdbcCommand("INSERT INTO jevslimporttemp(JevSLImportTempCode, JevSLImportTempDescription, JevSLImportTempAmount)VALUES('" + table.Rows[x][0].ToString() + "','" + table.Rows[x][1].ToString() + "'," + Convert.ToDouble(table.Rows[x][2].ToString()) + ");", conn);