我有 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();
}
任何提示或建议或正确方向的观点都会很棒!感谢大家!