当我尝试在名为' table'的数据表的mysql数据库数组值中插入时,我遇到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();
}
答案 0 :(得分:0)
您的变量中的值很可能会干扰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()));
答案 1 :(得分:0)
看起来你的所有三个字段都不是字符串类型。从字段名称我猜是第三个字段是数字。尝试不引用第三个字段:
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);