簡體   English   中英

使用多個條件的SQL Search查詢

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

我的問題是,我做了一個子表格進行搜索,但是我在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;

我在sql adapter命令和where子句命令中有錯誤,有什么幫助嗎?

三件事:

  1. 你在這行有錯字

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

    方法是AddWithValue (注意大小寫不同)

  2. SqlDataAdapter構造函數接受命令,但不連接,因為該命令已包含連接,所以這是正確的:

     SqlDataAdapter sda = new SqlDataAdapter(command); 

    可能最重要的最后一個:

  3. 如果您使用LIKE ,則需要使用通配符%

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM