我有一个搜索窗口,人员输入他们正在查找的内容,然后单击下一步。带有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