我有一個窗體窗體,用戶可以在多個文本框中輸入多個值以獲得更快的搜索結果。但是在運行時,它只需要第一個參數,即全名並忽略其他參數。不知道這是爲什麼。 我得到了我不想要的結果中的全表。無法將記錄從SQL表中提取到DataGridView在C#中
private void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=MADDY-PC\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
try
{
con.Open();
string sql = "SELECT * FROM Customers WHERE ContactName LIKE '%" + txtFullName.Text + "%' OR Address LIKE '%" + txtAddress.Text + " %' OR Phone LIKE '%"+txtContactNumber.Text+"%'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
da.Fill(ds);
if (ds.Rows.Count > 0)
{
dataGridView1.DataSource = ds;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
con.Close();
}
}
我在哪裏犯錯,請指導我。
感謝
檢查。沒有結果。還有其他選擇嗎? – user3095539
您是否在所有文本框中輸入文字?如果你錯過任何人,查詢條件語句將具有這個表達式'OR Address LIKE'%%'',這將導致在輸出 –
@DipenduPaul中選擇的所有記錄如何避免這種情況?如果用戶只輸入名稱並且只需要該名稱的結果或類似於該名稱的結果呢? – user3095539