我有一個asp.net網站,其中包含13個文本框,用戶可以輸入特定信息來搜索數據庫。一個gridview填充數據。數據僅用於查看目的。SQL語句不斷返回所有行而不是幾個
我的問題是,每一行總是返回。如果用戶僅將數據輸入到一個字段中,則只應返回包含該數據的行。相反,無論如何,每一行都會返回,我似乎無法弄清楚我的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();
}
任何提示或建議,或在正確的方向點是太棒了!感謝大家!
作爲一個建議,你應該使用'using'塊與實現IDisposable接口的類,特別是SqlConnection的,因爲如果異常生成,您的代碼將不會關閉連接。 –
也許你應該嘗試重寫SQL語句如果通過並且其餘結果將被返回,那麼知道使用'AND'和'OR'之間的區別 – MethodMan