我试图用一个文本框的文本在mysql数据库中选择一行。 但是,当我使用下面的代码时,我得到一个错误。C#的Mysql文本框文本选择
MySqlCommand command = connection.CreateCommand(); //we create a command
command.CommandText = "SELECT * FROM info where id=" + textBox1.Text ; //in commandtext, we write the Query
MySqlDataReader reader = command.ExecuteReader(); //execute the SELECT command, which returns the data into the reader
while (reader.Read()) //while there is data to read
{
MessageBox.Show(reader["info"].ToString());
}
它工作正常,用字母,但是当我尝试使用问号或类似的东西,我得到以下错误:‘?’
“参数必须定义。“在这种情况下
你可以张贴例外? –
MySql.Data.MySqlClient.MySqlException未处理 HResult = -2147467259 消息=参数'?'必须定义。 Source = MySql.Data ErrorCode = -2147467259 – user3540727
由于目前的情况,您的代码很容易受到SQL注入攻击。我强烈建议你考虑使用下面答案中给出的参数。 – mcy