【发布时间】:2014-03-06 09:28:43
【问题描述】:
我需要从Mysql 检索数据的代码很平静。如果我使用参数化查询,它不采用实际参数值,而是采用参数名称作为值。
错误:必须定义@choise
MySqlConnection connection = new MySqlConnection("");
MySqlDataAdapter mySqlDataAdapter;
DataSet DS;
private string columnValue = xxx;
private string Choise = yyy;
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM table2 WHERE " + columnValue + " = @choise";
command.Parameters.Add(new MySqlParameter("@choise", Choise));
DS = new DataSet();
connection.Open();
mySqlDataAdapter = new MySqlDataAdapter(command.CommandText, connection);
mySqlDataAdapter.Fill(DS);
connection.Close();
当我运行这个时,我得到如下查询:
SELECT * FROM table2 WHERE xxx = @choise
而不是
SELECT * FROM table2 WHERE xxx = yyy。
问题出在哪里?
我尝试过:
command.Parameters.Add(new MySqlParameter("@choise", Choise));
command.Parameters.AddWithValue("@choise", Choise);
当我使用实际变量而不是参数时它工作正常。
【问题讨论】: