0

我有 asp.net 站点,其中包含 13 个文本框,用户可以在其中输入某些信息以搜索数据库。网格视图填充数据。数据仅供查看。

我的问题是,每一行总是返回。如果用户只在一个字段中键入数据,则只应返回包含该数据的行。相反,无论如何都会返回每一行,我似乎无法弄清楚我的 SQL 语句有什么问题。

这是整个代码:

 SqlConnection mySqlConnection = new SqlConnection(strings.settings.connectionString);
    SqlCommand mycommand = new SqlCommand("SELECT SOPID, CONTACT, SHIPTONAME, KNOWN_EMAIL, ADDR1, ADDR2, CITY, STATE, ZIPCODE, PHONE1, CUSTPO, CID, AID, SEATS FROM dbo.LOOKUP_TEST_TBL WHERE CONTACT = @CONTACT OR SHIPTONAME = @SHIPTONAME OR KNOWN_EMAIL = @KNOWN_EMAIL OR ADDR1 = @ADDR1 OR ADDR2 = @ADDR2 OR CITY = @CITY OR STATE = @STATE OR ZIPCODE = @ZIPCODE OR PHONE1 = @PHONE1 OR CUSTPO = @CUSTPO OR CID = @CID OR AID = @AID OR SEATS = @SEATS", mySqlConnection);
    //AccessCommand.Parameters.AddWithValue("@ORDERID", SqlDbType.Char).Value = txtOrderID.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@CONTACT", SqlDbType.Char).Value = txtContact.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@SHIPTONAME", SqlDbType.Char).Value = txtShipToName.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@KNOWN_EMAIL", SqlDbType.Char).Value = txtEmail.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@ADDR1", SqlDbType.Char).Value = txtAddress1.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@ADDR2", SqlDbType.Char).Value = txtAddress2.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@CITY", SqlDbType.Char).Value = txtCity.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@STATE", SqlDbType.Char).Value = txtState.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@ZIPCODE", SqlDbType.Char).Value = txtZip.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@PHONE1", SqlDbType.Char).Value = txtPhone.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@CUSTPO", SqlDbType.Char).Value = txtCustomerPO.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@CID", SqlDbType.Char).Value = txtCustomerID.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@AID", SqlDbType.Char).Value = txtAddressID.Text.ToUpper();
    mycommand.Parameters.AddWithValue("@SEATS", SqlDbType.Char).Value = txtSeats.Text.ToUpper();
    mySqlConnection.Open();

    SqlDataReader reader = mycommand.ExecuteReader();
    if (reader.HasRows == false)
    {
        throw new Exception();
    }
    GridView1.DataSource = reader;
    GridView1.DataBind();
}

任何提示或建议或正确方向的观点都会很棒!感谢大家!

4

3 回答 3

3

你的查询应该看起来像

SELECT *  
FROM dbo.LOOKUP_TEST_TBL 
WHERE (CONTACT = @CONTACT and CONTACT is not null) or
      (SHIPTONAME = @SHIPTONAME and SHIPTONAME  is not null).....

只排除可为空的记录

于 2013-06-03T17:12:29.057 回答
1

问题几乎可以肯定是查询——它将返回在任何这些字段中具有空列的任何行,而用户没有提供这些行。

我会尝试仅使用用户指定的列来构建查询。

获取非空参数:

Dictionary<string, string> values = new Dictionary<string,string> {
    { "@CONTACT", txtContact.Text.ToUpper() },
    { "@ADDR1", txtAddress1.Text.ToUpper() },
    // etc
};
var enteredParams = values.Where(kvp => !string.IsNullOrEmpty(kvp.Value));

并构造查询:

string sql = string.Format("SELECT ... WHERE {0}",
    string.Join(" OR ", 
        enteredParams.Select(kvp => 
            kvp.Key.TrimStart(new char[] { '@' }) + " = " + kvp.Key
        )
    );

最后构造参数:

foreach (var kvp in enteredParams)
{
    mycommand.Parameters.AddWithValue(kvp.Key, kvp.Value);
}     

(未检查确切的语法,但我想你明白了。)

于 2013-06-03T17:09:39.507 回答
0

您将 WHERE 子句中的所有内容组合在一起,因此即使其中之一匹配,它也会包含在结果中。你是不是想把它们放在一起?

于 2013-06-03T17:09:02.007 回答