简体   繁体   English

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

[英]SQL Syntax error when trying to insert array values of a Datatable

I am having a SQL syntax error when I try to Insert in mysql database array values of a datatable named 'table'. 当我尝试在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; 数据表=表;

Here's the code: 这是代码:

 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();
   }

You most likely have values in your variable interfering with the sql, like a single quote or question mark. 您的变量中很可能会有值干扰sql,例如单引号或问号。 You should parameterize your query. 您应该参数化您的查询。

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()));

It looks like all three of your fields are not string type. 您的所有三个字段似乎都不是字符串类型。 From the field names my guess is the third field is numeric. 从字段名称来看,我猜第三个字段是数字。 Try without quote for the third field: 尝试在第三字段不加引号:

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);

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

相关问题 为什么在尝试将数据插入行时出现 SQL 语法错误? - Why am I getting an SQL Syntax error when trying to insert data into a row? SQL-尝试增加值时的语法错误 - SQL - Syntax Error When Trying to Increment a Value 尝试在数据库中输入值时INSERT INTO语句中的语法错误 - Syntax error in INSERT INTO Statement while trying to input values in the Database 尝试向数据库插入行时,SQL语法无效 - Invalid SQL syntax when trying to insert a row to the database SQL语法错误(INSERT命令) - SQL Syntax Error (INSERT command) 尝试插入带引号的数据时,sqlite插入查询中出现语法错误 - Syntax error in sqlite insert query when trying to insert data with quote symbol 尝试将图像保存到Access中:“ INSERT INTO中的语法错误” - Trying to save an image to Access: “Syntax error in INSERT INTO” 将数据表插入/更新到SQL Server错误:列名或提供的值数与表定义不匹配 - Insert/Update DataTable to SQL Server ERROR:Column name or number of supplied values does not match table definition 我正在尝试插入文本框值,但它一直在USER附近显示&#39;语法错误? - I'm trying to insert textboxes values but it keeps showing ' syntax error near USER? 我试图在sql server中插入一个表,但它给出了关键字'of'附近的错误语法错误 - I am trying to insert into a table in sql server but its giving an error that Incorrect Syntax near the keyword 'of'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM