按钮上的sql select命令出现问题
本文关键字:问题 命令 select sql 按钮 | 更新日期: 2023-09-27 18:29:14
我有一个名为app的数据库,我想从我的数据库表簿中创建一个用于搜索的表单。我做了一个列表框,里面有"作者"answers"书名",还有一个文本框,用户可以在其中键入他们想要搜索的作者或书名。这是我的代码:
string constring = "datasource=localhost;port=3306;username=root;password=****;";
MySqlConnection conDatabase = new MySqlConnection(constring);
string mySelectQuery = "";
MySqlDataAdapter da = new MySqlDataAdapter(mySelectQuery, constring);
if (listBox3.SelectedIndex == 0)
{
mySelectQuery = "select * from apps.books where book_author LIKE @name ";
}
else if (listBox3.SelectedIndex == 1)
{
mySelectQuery = "select * from apps.books where book_name LIKE @name ";
}
da.SelectCommand.Parameters.AddWithValue("@name", "%" + textBox1.Text + "%");
MessageBox.Show(mySelectQuery);
conDatabase.Close();
问题是,当我按下搜索按钮时,消息显示"从(…)中选择*",而不是我要查找的表的组件。你能帮我吗?
您应该移动这一行
MySqlDataAdapter da = new MySqlDataAdapter(mySelectQuery, constring);
在if语句下面。就像现在一样,在修改字符串之前,您可以使用字符串中的任何内容进行查询
您应该添加一个检查,以确保选择索引是您期望的中的两个索引之一
else
{
mySelectQuery = "Select 'Invalid dropdown selection'";
}