if (textBox1.Text.Trim() != "")
str = "where id like " + textBox1.Text.Trim();
try
{
string searchStr = ("select * from student " + str);
MySqlDataAdapter adapter = new MySqlDataAdapter(searchStr, SQLCon);
DataTable a = new DataTable();
adapter.Fill(a);
this.dataGridView1.DataSource = a;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
}
注意一下下面两个sql的区别
select * from student where id = 'abc123';
select * from student where id = abc123 ;
上面两个sql中,第2条是有问题的,sql里的字符串,需要使用单引号包裹,否则就会被认为是列名或参数名什么的,而非具体的值。
所以你把你拼接的sql稍微再改下就行了