SQL 服务器包含谓词不适用于多个搜索词
SQL Server contains predicate not working with multiple search terms
我有一个 c# 程序可以根据用户的输入搜索 table。
关键词被space分割后存入数组
然后 switch 语句将 select 基于仅输入一个词或两个词的正确大小写。
我的 switch 语句只为第一种情况填充我的数据网格,但是当尝试使用第二种情况时,我的程序会捕获异常。
我试过调试,但我唯一看到的是当我进入案例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): Incorrect syntax near
'@words1'. Error Number:102,State:1,Class:15
我认为这是 switch case 已知的作用域问题,因为历史上 reasons.That 为什么我们必须在每个语句之后放置 break。试试看这里。
Variable declaration in a C# switch statement
您现有代码不起作用的原因是 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]
我有一个 c# 程序可以根据用户的输入搜索 table。
关键词被space分割后存入数组
然后 switch 语句将 select 基于仅输入一个词或两个词的正确大小写。
我的 switch 语句只为第一种情况填充我的数据网格,但是当尝试使用第二种情况时,我的程序会捕获异常。
我试过调试,但我唯一看到的是当我进入案例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): Incorrect syntax near '@words1'. Error Number:102,State:1,Class:15
我认为这是 switch case 已知的作用域问题,因为历史上 reasons.That 为什么我们必须在每个语句之后放置 break。试试看这里。 Variable declaration in a C# switch statement
您现有代码不起作用的原因是 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]