简体   繁体   English

使用多个条件的SQL Search查询

[英]an SQL Search query using more than one condition

my problem is that , i made a child form for searching , but i have problem in sql query and parameters , my code is 我的问题是,我做了一个子表格进行搜索,但是我在sql查询和参数中遇到了问题,我的代码是

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;

i have error in sql adapter command and in where clause command , any help ?? 我在sql adapter命令和where子句命令中有错误,有什么帮助吗?

Three things: 三件事:

  1. You have a typo on this line 你在这行有错字

     command.Parameters.Addwithvalue("@name", nametxt.Text); 

    The method is AddWithValue (note the case difference) 方法是AddWithValue (注意大小写不同)

  2. The constructor of SqlDataAdapter takes the command but no connection then since the command already contains the connection, so this is correct: SqlDataAdapter构造函数接受命令,但不连接,因为该命令已包含连接,所以这是正确的:

     SqlDataAdapter sda = new SqlDataAdapter(command); 

    Probably the most important last: 可能最重要的最后一个:

  3. If you use LIKE you need to use the wild-cards % : 如果您使用LIKE ,则需要使用通配符%

     command.Parameters.AddWithValue("@name", string.Format("%{0}%",nametxt.Text); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM