以下代码:
string query = "INSERT INTO `@tableName` (@tableKeys) VALUES(@tableValues)";
MySqlCommand sql = new MySqlCommand(query);
sql.Connection = connection;
sql.Parameters.Add(new MySqlParameter("@tableName", values[0]));
sql.Parameters.Add(new MySqlParameter("@tableKeys", values[1]));
sql.Parameters.Add(new MySqlParameter("@tableValues", values[2]));
sql.ExecuteNonQuery();
返回错误:
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''porto,date') VALUES('232,17-11-2014 10:48:11')' at line 1
我使用以下代码调用代码:
string[] values = {"logs","porto,date",num+","+date};
bool dbopen = dbManager.OpenConnection();
if(dbopen){
dbManager.Insert(values);
}
谁能告诉我我做错了什么?
最佳答案
您无法参数化表名称和列名称。
您只能参数化您的值。您需要添加表名和列名作为 sql 查询的一部分。
您应该在将表名放入 SQL 之前对其执行非常严格的验证,或者将一组有效表名列入白名单,以避免以正常方式进行 SQL 注入(inject)攻击。
关于c# - MySql.Data错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26969953/