关于SQL查询语句,不知道是哪里出错了,帮我看看!C#

来源:百度知道 编辑:UC知道 时间:2024/05/17 22:36:28
OleDbConnection thisConnection = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = h:\Books.mdb"); OleDbCommand thisCommand = new OleDbCommand("select * from Titles where publisherID like 'textBox1.Text%'", thisConnection);
OleDbDataAdapter thisAdapter = new OleDbDataAdapter(thisCommand);
DataSet booksDataSet = new DataSet();
thisAdapter.Fill(booksDataSet, "Titles");
this.dataGridView1.DataSource = booksDataSet.Tables["Titles"];

我想在textBox1中输入模糊查询的条件,然后单击button查询,以上是我button1_Click的代码,我在textBox1中输入031,然后单击button,但是在dataGridView1中不显示任何东西,但是如果我把'textBox1.Text%'改为'031%',所有包含031的记录都能在dataGridView1中显示,估计就是'textBox1.Text%'出错,需要怎么改?

OleDbConnection thisConnection = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = h:\Books.mdb"); OleDbCommand thisCommand = new OleDbCommand("select * from Titles where publisherID like '"+textBox1.Text+"%'", thisConnection);
OleDbDataAdapter thisAdapter = new OleDbDataAdapter(thisCommand);
DataSet booksDataSet = new DataSet();
thisAdapter.Fill(booksDataSet, "Titles");
this.dataGridView1.DataSource = booksDataSet.Tables["Titles"];

改成OleDbCommand thisCommand = new OleDbCommand("select * from Titles where publisherID like '%"+textBox1.Text+"%'", thisConnection); 再试哈子。