我正在编写一段非常简单的代码,但无法弄清楚为什么这些参数对我的 SQL 选择不起作用。任何建议将不胜感激。
- 我之前有一个不带参数的连接/选择,工作正常
- 我已经尝试过MySQL工作台中的选择,它很好(并且也简化了它) 这是相关代码。 ExecuteReader 语句出现异常。
这是本地窗口的结果:
- [MySql.Data.MySqlClient.MySqlException] {"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 ''AAIT_N'' at line 1"} MySql.Data.MySqlClient.MySqlException
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand cmd;
conn.Open();
try
{
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * from @p ";
cmd.Parameters.AddWithValue("@p", sym);
string wait = Console.ReadLine();
MySqlDataReader myresults = cmd.ExecuteReader(); <=== Throws an exception
if (myresults.HasRows)
etc.
最佳答案
不,您不能将表名称作为参数传递。相反,你可以像下面这样做
string query = string.Format("SELECT * from {0}", sym);
cmd.CommandText = query;
MySqlDataReader myresults = cmd.ExecuteReader();
关于c# - 将表名作为参数传递给 MySqlCommand 时出现 MySqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31791356/