问题描述
- 求助大神 SQL怎么在C#里面用不了LIKE查询
-
请帮帮忙string select_sql;
string connect_sql = "Data source=.; initial catalog=project; integrated security=true";
select_sql = "use project select ISBN,BKNAME,AUTHOR,PUBLISHER,PRICE,CNUM,SNUM from book where @libname LIKE bkname";
SqlConnection sc = new SqlConnection(connect_sql);
SqlCommand cmd = new SqlCommand(select_sql, sc);
SqlParameter snoparam = new SqlParameter("@libname", textBox1.Text);
cmd.Parameters.Add(snoparam);
SqlDataAdapter da = new SqlDataAdapter();
// da.Fill(ds, "grde");
da.SelectCommand = cmd;
DataSet pro = new DataSet("project");
pro.Tables.Add("jy");
da.Fill(pro, "jy");
dataGridView1.DataSource = pro.Tables["jy"];
sc.Close();
解决方案
select_sql = "use project select ISBN,BKNAME,AUTHOR,PUBLISHER,PRICE,CNUM,SNUM from book where @libname LIKE bkname";
->
select_sql = "select ISBN,BKNAME,AUTHOR,PUBLISHER from book where bkname LIKE '%@libname%'";
解决方案二:
LIKE后的要用到单引号或者通配符
解决方案三:
定义的sql查询字符串写的有问题,应改为
select_sql = "select ISBN,BKNAME,AUTHOR,PUBLISHER from book where bkname LIKE '@libname'"
bkname和@libname的位置要换一下,并加引号,bkname为对应sql里的子段名字,后者为引用的值
时间: 2024-11-30 15:08:21