简体   繁体   中英

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

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 ??

Three things:

  1. You have a typo on this line

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

    The method is AddWithValue (note the case difference)

  2. The constructor of SqlDataAdapter takes the command but no connection then since the command already contains the connection, so this is correct:

     SqlDataAdapter sda = new SqlDataAdapter(command); 

    Probably the most important last:

  3. If you use LIKE you need to use the wild-cards % :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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