我有一個搜索窗口,人員輸入他們正在查找的內容,然後單擊下一步。帶有like和參數的SQL語句
在我的sql語句中,我想讓我的參數搜索到我的表BILNAME和CUSTOMER。
我添加了Like,因爲我希望我的客戶端只輸入2個字母,並且它看起來無處不在。
這裏是我有什麼,現在,我的循環直接進入錯誤消息時我調試我看到我的表數爲0
我的猜測是,這個問題來自於SQL語句。
private void btn_Next_Click(object sender, EventArgs e)
{
string connectionstring = "...";
string sql = ("SELECT * FROM C3 WHERE BILNAME LIKE @search OR CUSTOMER LIKE @search");
mycommand = new SqlCommand(sql, connection);
mycommand.Parameters.AddWithValue("@search", "%" + clsDataSource.search + "%");
myadapt = new SqlDataAdapter(mycommand);
myset = new DataSet();
myadapt.Fill(myset, "C3");
mytable = myset.Tables["C3"];
if (mytable.Rows.Count == 1)
{
DisplayWindow displaywindow = new DisplayWindow();
displaywindow.ShowDialog();
this.Close();
}
else if (mytable.Rows.Count > 1)
{
GUI.Multipleresult multipleresultDisplay = new GUI.Multipleresult();
multipleresultDisplay.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("Nothing found, please try again", "No result", MessageBoxButtons.OK, MessageBoxIcon.Error);
SearchWindow searchWindowDisplay = new SearchWindow();
searchWindowDisplay.ShowDialog();
this.Close();
}}
您需要使用單引號包圍它例如LIKE'%searchvalue%' – Kevin
@Kevin - 不,你不知道。你這樣做後,搜索將在開始和結束時過濾實際字符'''。 – Igor
@Igor只有當單引號在%之內時,它們上的LIKE過濾器纔會被加倍 – Kevin