访问查询和运算符不起作用的问题
我有一个搜索表单,用户可以在其中输入并搜索记录。搜索栏中的搜索关键字的形式为:smooth,wood,38 现在实际的问题是,当用户输入一个关键字时,返回一条记录,当用户输入第二个关键字时,返回一条记录,但也返回与搜索不匹配的旧记录。 如果他输入 38,wood,那么会返回两条记录,但是当他输入红色时,逻辑上应该只返回一条记录,但事实并非如此,它也返回了蓝色记录。< /强> *每次用户输入搜索关键字或第二个关键字、第三个关键字时,应根据搜索词过滤结果,但不幸的是,它并没有这样做*。 table1结构如下:
id path name search_keyword
1 c:\my picture\ red door red;
2 c:\my picture\ red door 38;
3 c:\my picture\ red door wood;
4 c:\my picture\ red door 2500;
5 c:\my picture\ red door smooth
6 c:\my picture\ blue door blue ;
7 c:\my picture\ blue door 38;
8 c:\my picture\ blue door wood;
9 c:\my picture\ blue door 2600;
19 c:\my picture\ blue door smooth;
该表包含搜索关键字,用户可以在其中输入search_keyword,可以得到结果过滤器(多个搜索关键字); 假设他输入起始 smooth,wood 38,那么当他输入 red/blue 或 2600/2500 时,结果应该显示为 1。
查询看起来像这样:
select Distinct name from table1 where search_keyword like '%smooth%' And '% wood %' and '%red%' order by name
以下代码:
publi override list<result> retrunsearch(string search)
{
string[] search = pQuery.Split(',');
List <result> myresult = new List<result>();
for (int i = 1; i < search.Length; i++)
{
StringBuilder query = new StringBuilder();
query.Append(
string.Format("SELECT DISTINCT name,path FROM table1 WHERE search_keyword LIKE '%{0};%'", search[0])
);
// Add extra keywords
for (int i = 1; i < search.Length; i++)
{
query.Append(string.Format(" And '%{0};%'", search[i]));
}
// Add order by
query.Append(" ORDER BY name");
using (OleDbCommand command = new OleDbCommand(query.ToString(), sqlconConnection))
{
command.CommandType = CommandType.Text;
using (OleDbDataReader sdaResult0 = command.ExecuteReader())
{
while (sdaResult0.Read())
{
result restult1= new result();
result1.name = sdaResult0.String(0);
myresult.add(result1);
}
sdaResult0.Close();
}
}
return myresult;
}
public class result{
public result()
{
}
public string name{get;set;}
}
i have a search form where user can type in and search for records. the search keyword in the search bar are in this form: smooth,wood,38
Now the actual problem is that, when user type in for one keyword a record is returned and when he type in for a second keyword, a record is returned but the old record which is not matching to the search is also returned.
if he type in for 38,wood so two records are returned but when he type in red, logically only one record should be returned but it dont and it returned the blue one as well.
*Every time the user type in for a search_keyword or 2nd keyword,3rd keyword the result should be filtered according to the search word and unfortunately it is not doing like that*.
table1 structure like this:
id path name search_keyword
1 c:\my picture\ red door red;
2 c:\my picture\ red door 38;
3 c:\my picture\ red door wood;
4 c:\my picture\ red door 2500;
5 c:\my picture\ red door smooth
6 c:\my picture\ blue door blue ;
7 c:\my picture\ blue door 38;
8 c:\my picture\ blue door wood;
9 c:\my picture\ blue door 2600;
19 c:\my picture\ blue door smooth;
this table contain the search keyword where the user can type in for search_keyword and can get the result filter (multiple search key-word);
suppose he type in starting smooth,wood 38 so both the result should be displayed now when he type red/blue or 2600/2500 then the result should be one.
the query look like this:
select Distinct name from table1 where search_keyword like '%smooth%' And '% wood %' and '%red%' order by name
following code:
publi override list<result> retrunsearch(string search)
{
string[] search = pQuery.Split(',');
List <result> myresult = new List<result>();
for (int i = 1; i < search.Length; i++)
{
StringBuilder query = new StringBuilder();
query.Append(
string.Format("SELECT DISTINCT name,path FROM table1 WHERE search_keyword LIKE '%{0};%'", search[0])
);
// Add extra keywords
for (int i = 1; i < search.Length; i++)
{
query.Append(string.Format(" And '%{0};%'", search[i]));
}
// Add order by
query.Append(" ORDER BY name");
using (OleDbCommand command = new OleDbCommand(query.ToString(), sqlconConnection))
{
command.CommandType = CommandType.Text;
using (OleDbDataReader sdaResult0 = command.ExecuteReader())
{
while (sdaResult0.Read())
{
result restult1= new result();
result1.name = sdaResult0.String(0);
myresult.add(result1);
}
sdaResult0.Close();
}
}
return myresult;
}
public class result{
public result()
{
}
public string name{get;set;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您要求 Access 的数据库引擎运行此查询时……
我认为它给您的结果将与您要求它运行此查询相同……
您必须告诉它您在哪个字段对于 WHERE 子句中的每个 AND 条件,进行比较以及您想要的比较类型。
但是,查看 table1 的示例数据,没有记录包含 search_keyword 字段中的所有三个搜索字符串。因此,也许您确实希望在这些 WHERE 子句条件之间使用 OR 而不是 AND:
我还将名称括在方括号中,因为名称是 MS Access 中的保留字。
编辑:另外,我想知道你是否真的想要木材前后的空格。如果没有,请尝试以下方式:
Edit2:由于您的评论表明您可以直接在 Access 中尝试查询,因此请尝试这两个查询,并让我们知道当您在 Access 而不是从 C# 中运行它们时会发生什么。
根据您向我们展示的示例数据,第一个查询不应返回任何记录,第二个查询应返回几条记录。
When you ask Access' database engine to run this query ...
... I think the results it gives you will be the same as if you asked it to run this query ...
You have to tell it which field you're comparing against, and the type of comparison you want, for each ANDed condition in the WHERE clause.
However, looking at the sample data for table1, no records contain all three of your search strings in the search_keyword field. So perhaps you really want OR instead of AND between those WHERE clause conditons:
I also enclosed name in square brackets because name is a reserved word in MS Access.
Edit: Also, I wonder whether you really want the spaces before and after wood. If not, try it this way:
Edit2: Since your comment indicated you can try queries in Access directly, try these two and let us know what happens when you run them in Access instead of from c#.
Based on the sample data you showed us, the first query should return no records and the second should return several.