SQL Server包含无法与多个搜索词一起使用的谓词

时间:2018-11-28 20:03:56

标签: c# sql-server full-text-search

我有一个c#程序,可通过用户输入搜索表。

关键字被空格分隔并保存到数组中。

然后switch语句将根据仅输入一个或两个单词来选择正确的大小写。

我的switch语句仅在第一种情况下填充我的数据网格,但是在尝试使用第二种情况时,我的程序转到catch异常。

我尝试调试,但是唯一看到的是,当我进入案例2时,它不会超出sda1.Fill(dt1);

更新的代码:

static string myconnstr = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;       

private void btnSearch_Click(object sender, EventArgs e)
{
    //Get the value from textbox
    string keyword = txtboxKeyword.Text;
    string[] words = keyword.Split(' ');

    //SQL Connection
    var conn = new SqlConnection(myconnstr);

    try
    {
        switch (words.Length)
        {
            case 1:
                    //Declare Command object with parameter
                    SqlCommand cmd = new SqlCommand("SELECT Site, StreetAddress, City, State, Zip, PharmacyPhone, MDVersion, InstallDate, SiteCodes, SiteNotActive, CloseDate, SiteNotes " +
                                    "FROM Sites WHERE contains(site, @words0) OR contains (StreetAddress, @words0) OR contains(city, @words0)", conn);

                    cmd.Parameters.AddWithValue("@words0", words[0]);

                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    dataGridSites.ReadOnly = true;
                    dataGridSites.DataSource = dt;
                    dataGridSites.CurrentCell = null;
                    break;
                case 2:
                    //Declare Command object with parameter
                    SqlCommand cmd1 = new SqlCommand("SELECT Site, StreetAddress, City, State, Zip, PharmacyPhone, MDVersion, InstallDate, SiteCodes, SiteNotActive, CloseDate, SiteNotes " +
                                    "FROM Sites WHERE contains(site, @words0, @words1) OR contains (StreetAddress, @words0, @words1) OR contains(city, @words0, @words1)", conn);

                    cmd1.Parameters.AddWithValue("@words0", words[0]);
                    cmd1.Parameters.AddWithValue("@words1", words[1]);

                    SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
                    DataTable dt1 = new DataTable();
                    sda1.Fill(dt1);
                    dataGridSites.ReadOnly = true;
                    dataGridSites.DataSource = dt1;
                    dataGridSites.CurrentCell = null;
                    break;                   
        }
    }
    catch (Exception)
    {
        MessageBox.Show("Search cannot be blank.");
    }            
}

这是我得到的更新的异常错误:

  

System.Data.SqlClient.SqlException(0x80131904):附近的语法不正确   '@ words1'。错误号:102,状态:1,类别:15

2 个答案:

答案 0 :(得分:0)

现有代码无法正常工作的原因是contains不支持您当前尝试调用它的方式中的三个参数。

根据documentation,我建议更改:

FROM Sites WHERE contains(site, @words0) OR contains (StreetAddress, @words0) OR contains(city, @words0)

收件人:

FROM Sites WHERE contains((site, StreetAddress, city), @words0)

并且:

FROM Sites WHERE contains(site, @words0, @words1) OR contains (StreetAddress, @words0, @words1) OR contains(city, @words0, @words1)

收件人:

FROM Sites WHERE contains((site, StreetAddress, city), @words0) OR contains((site, StreetAddress, city), @words1)

如果您真的想使用当前更详细的样式,则Example I建议:

FROM Sites WHERE contains(site, @wordsConcat) OR contains (StreetAddress, @wordsConcat) OR contains(city, @wordsConcat)

可能有效,其中@wordsConcat已(通过C#)设置为:

words[0] + " OR " + words[1]

答案 1 :(得分:-6)

由于历史原因,我认为这是开关案例中的一个范围问题。这就是为什么我们必须在每个陈述后都放个空格。尝试在这里看看。 Variable declaration in a C# switch statement