【发布时间】:2018-09-30 08:15:18
【问题描述】:
当我使用参数化 sql 查询时,我只是不断收到语法错误。
public List<string> Cat(string product,string table)
{
List<string> Products = new List<string>();
Global global = new Global();
string sql = "SELECT @prod FROM @tbl";
MySqlConnection connection = new MySqlConnection(global.ConnectionString);
MySqlCommand command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@prod", product);
command.Parameters.AddWithValue("@tbl", table);
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
Products.Add(reader.GetString("@prod"));
}
connection.Close();
return Products;
}
public List<string> CallProducts(string category)
{
string table;
string product;
List<string> stacks = new List<string>();
if (category == "Accessories")
{
product = "Accessories_Name";
table = "tbl_accessories";
stacks.AddRange(Cat(product, table).ToArray());
}
else if (category == "Batteries")
{
table = "tbl_batteries";
}
else if (category == "Cotton")
{
table = "tbl_cotton";
}
else if (category == "Juices")
{
table = "tbl_juices";
}
else if (category == "Kits")
{
table = "tbl_kits";
}
else if (category == "Mods")
{
table = "tbl_mods";
}
else
{
table = "tbl_vapeset";
}
return stacks;
}
我只是不断收到 SQL 语法错误。如果手动输入表格和名称而不是使用参数,则它可以工作。
希望您能提供帮助。
需要一个项目。
谢谢!
【问题讨论】:
-
专业提示我们看不到您的屏幕,因此必须将错误复制进去
-
还有
public List<string> Cat(string product, string table) -
您好专业提示,您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“tbl_accessories”附近使用正确的语法。
-
这是我得到的错误。