Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有like和参数的SQL语句_C#_Sql - Fatal编程技术网

C# 具有like和参数的SQL语句

C# 具有like和参数的SQL语句,c#,sql,C#,Sql,我有一个搜索窗口,用户输入他们要查找的内容,然后单击“下一步” 在我的sql语句中,我希望我的参数搜索到我的表BILNAME和CUSTOMER 我添加了Like,因为我希望我的客户只输入2个字母,而且它在单词中到处都是 这是我的情况,现在,当我调试时,我的循环直接转到错误消息,我看到我的表计数为0 我猜问题来自SQL语句 private void btn_Next_Click(object sender, EventArgs e) { string connectionstring =

我有一个搜索窗口,用户输入他们要查找的内容,然后单击“下一步”

在我的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();
   }}

您可能想使用关键字


真不敢相信我竟然忘了!非常感谢。当我使用那个SQL语句时,我的if语句不起作用,如果我有一个结果,它会直接进入第二个循环,应该是第二个循环not@Cbz“我的if语句不起作用”是什么意思?您显示的代码中没有循环。我的if语句不起作用。它不能正确计数。您的SQL语句在ManagementStudio中工作吗?你在那里得到什么结果了吗?你需要用单引号括起来,例如“%searchvalue%”@Kevin-不,你没有。如果这样做,搜索将在开始和结束时过滤实际字符
。@Igor仅当单引号在%内时,才会对它们进行类似的过滤,然后必须将它们加倍up@Kevin-这是不正确的。使用参数时,不添加额外的单引号。参数应提供直接字符串值。参数值为
'%searchvalue%'
将限制搜索以
'
字符开头和结尾的表值(以及中间部分%searchvalue%,我认为这一点没有争议)。
SELECT * FROM C3 WHERE BILNAME LIKE @search OR CUSTOMER LIKE @search