我要从数据库中采用模糊查询.......当在textBox1中写出需要查询的书名信息后...然后从数据库中提取出信息....
显示在windows窗体中..
该怎么做....
下面的代码我运行了,可不对...提示like附近语法错误...
- C# code
SqlConnection cn = new SqlConnection("server=localhost;database=Labiary;integrated security=true"); DataSet ds = new DataSet(); String name = textBox1.Text.Trim(); SqlCommand cmd = new SqlCommand("select 书名 from Books where 书名 like" + name + "", cn); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(ds); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { Console.WriteLine(ds.Tables[0].Rows[i][j].ToString()); DataTable dt = ds.Tables[0]; this.dataGridView1.DataSource = dt; } }
------解决方案--------------------
SqlCommand cmd = new SqlCommand("select 书名 from Books where 书名 like" + name + "", cn);
改成:
SqlCommand cmd = new SqlCommand("select 书名 from Books where 书名 like '%" + name + "%'", cn);
试试看