在SELECT语句中使用WHERE两次

时间:2016-03-13 05:56:09

标签: jquery asp.net select where

我有一个搜索和

这是我按BUTTON点击搜索时的代码

void GetProducts()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT Products.ProductID, Products.Name, Categories.Category, " +
        "Products.Code, Products.Image, Products.Description, Products.Price, " +
        "Products.IsFeatured, Products.DateAdded, Products.DateModified, Products.Status, Products.Available, Products.Criticallevel " +
        "FROM Products INNER JOIN Categories ON Products.CatID = Categories.CatID WHERE Products.Available <= Products.Criticallevel";
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "Products");
    lvProducts.DataSource = ds;
    lvProducts.DataBind();
    con.Close();
}

void GetProducts1(string keyword)
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT Products.Available, Products.Criticallevel, Products.ProductID, Products.Name, Categories.Category, " +
        "Products.Code, Products.Image, Products.Description, Products.Price, " +
        "Products.IsFeatured, Products.DateAdded, Products.DateModified, Products.Status " +
        "FROM Products INNER JOIN Categories ON Products.CatID = Categories.CatID " +
        "WHERE Products.ProductID LIKE '%" + keyword + "%' OR Products.Name LIKE '%" + keyword + "%' " +
        " OR Categories.Category LIKE '%" + keyword + "%' OR Products.Code LIKE '%" + keyword + "%' " +
        " OR Products.Description LIKE '%" + keyword + "%' OR Products.Price LIKE '%" + keyword + "%' OR Available <= Criticallevel";
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "Products");
    lvProducts.DataSource = ds;
    lvProducts.DataBind();
    con.Close();
}

protected void btnSearch_Click1(object sender, EventArgs e)
{
    if (txtSearch.Text.Trim() == "")
    {
        GetProducts();
    }
    else
    {
        GetProducts1(txtSearch.Text);
    }
}

查看页面时,WHERE可以正常工作。

但每当我使用搜索和点击按钮时,

哪里不起作用。

所以这是我的页面(只是信息)

enter image description here

我搜索时应该显示相同的消息No Records Found 因为WHERE下没有产品。

但是当搜索(btn点击)时,

enter image description here

产品展示。

请帮帮我们! 提前谢谢你

1 个答案:

答案 0 :(得分:2)

你把所有东西都保存在OR状态是错误的。条件应该是这样的 WHERE Products.Available <= Products.Criticallevel AND (condition1 or condition2 or condition3)

void GetProducts1(string keyword)
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT Products.Available, Products.Criticallevel, Products.ProductID, Products.Name, Categories.Category, " +
        "Products.Code, Products.Image, Products.Description, Products.Price, " +
        "Products.IsFeatured, Products.DateAdded, Products.DateModified, Products.Status " +
        "FROM Products INNER JOIN Categories ON Products.CatID = Categories.CatID " +
        "WHERE Available <= Criticallevel AND (Products.ProductID LIKE '%" + keyword + "%' OR Products.Name LIKE '%" + keyword + "%' " +
        " OR Categories.Category LIKE '%" + keyword + "%' OR Products.Code LIKE '%" + keyword + "%' " +
        " OR Products.Description LIKE '%" + keyword + "%' OR Products.Price LIKE '%" + keyword + "%')";
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "Products");
    lvProducts.DataSource = ds;
    lvProducts.DataBind();
    con.Close();
}