I am having a SQL syntax error when I try to Insert in mysql database array values of a datatable named 'table'.
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. 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.