C# 使用多个条件的SQL搜索查询
我的问题是,我制作了一个用于搜索的子表单,但我在sql查询和参数方面有问题,我的代码是C# 使用多个条件的SQL搜索查询,c#,sql,where,sqlparameter,C#,Sql,Where,Sqlparameter,我的问题是,我制作了一个用于搜索的子表单,但我在sql查询和参数方面有问题,我的代码是 SqlConnection sc = new SqlConnection( "Data Source=MOHAMMED-PC;Initial Catalog=salessystem;Integrated Security=True"); SqlCommand command = new SqlCommand( "Select * from customers WHERE (docno = @
SqlConnection sc = new SqlConnection(
"Data Source=MOHAMMED-PC;Initial Catalog=salessystem;Integrated Security=True");
SqlCommand command = new SqlCommand(
"Select * from customers WHERE (docno = @doc) OR (NAME LIKE @name ) OR (salepoint = @salepoint)", sc);
DataTable dt = new DataTable();
command.Parameters.AddWithValue("@doc", doctxt.Text);
command.Parameters.Addwithvalue("@name", nametxt.Text);
command.Parameters.AddWithValue("@salepoint", salepointtxt.Text);
SqlDataAdapter sda = new SqlDataAdapter(command, sc);
sda.Fill(dt);
dataGridView1.DataSource = dt;
我在sql适配器命令和where子句命令中有错误,是否有任何帮助???三件事:
你这行有错
command.Parameters.Addwithvalue("@name", nametxt.Text);
方法为AddWithValue,注意大小写差异
由于该命令已包含连接,因此接受该命令但没有连接,因此这是正确的:
SqlDataAdapter sda = new SqlDataAdapter(command);
可能是最重要的最后一点:
如果您像这样使用,则需要使用通配符%:
请您发布错误消息好吗?检查数据类型也可以吗。所有测试?让你怀疑这是否是给OP一个错误的代码。@ryadavilli:至少这是两个编译时错误,他在适配器命令中提到了两个错误。他也不应该从LIKE子句中得到任何结果,他在哪里使用SQL%通配符?@DJKRAZE:是的,你是对的。据此编辑了我的答案。你知道如何格式化我的回答,使数字连续吗?我更改了sql适配器命令,使用了通配符,效果很好,非常感谢
command.Parameters.AddWithValue("@name", string.Format("%{0}%",nametxt.Text);