[英]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.