gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:48:52 25 4
gpt4 key购买 nike

以下代码:

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)攻击。

阅读The Curse and Blessings of Dynamic SQL

关于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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com