多个搜索关键字的问题 [英] problem with multiple search keywords

查看:81
本文介绍了多个搜索关键字的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,在该应用程序中,用户可以使用单个关键字或多个关键字进行搜索.我已经使用了每种技术,但是我不知道这段代码有什么问题,因为它不过滤结果并继续添加新的结果. 搜索关键字以逗号分隔,例如 summer,38,blue ,这是3个关键字.该表的代码和结构如下所示.

i have web-application, in the application a user can search by using a single keyword or multiple keyword. i have used every technique but i do not know what is wrong with this code as it do not filter the result and continue adding new result. the search keywords are seperated by comma, like summer,38,blue these are 3 keywords. the code and structure of the table is give below.

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++)
                {

  where += " And '%" + search[i] + "%'";
  OleDbCommand sqlcmdCommand0 = new OleDbCommand("select Distinct name from table1 where     search like '%" + search[0] + "%' " + where + " order by name", sqlcon);
                sqlcmdCommand0.CommandType = CommandType.Text;
                OleDbDataReader sdaResult0 = sqlcmdCommand0.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;}
}

the structure of the table is: 
id      name           keyword;
1       blue  jeans      blue;
2       blue  jeans      38;
3       blue jeans       summer;
4       black jeans      black;
5       black jeans      38;
6       black jeans      summer; 

推荐答案

第二步是使用一个或多个关键字来检索记录.我添加了一些更好的变量名和格式以及一些语法提示,以帮助提高可读性.

Second go at retrieving records using one or more keywords. I've added some nicer variable names and formatting along with some syntax tips to help with readability.

public override List<string> Search(string pQuery)
{
    string[] keywords = pQuery.Split(',');
    List<string> results = new List<string>();

    if (keywords.Length == 0)
    {
        // Code expects at least one keyword - throw exception or return null ?
    }

    StringBuilder query = new StringBuilder();
    query.Append(
        string.Format("SELECT DISTINCT name FROM table WHERE keyword LIKE '%{0}%'", keywords[0])
    );

    // Add extra keywords
    if (keywords.Length > 1)
    {
        for (int i = 1; i < keywords.Length; i++)
        {
            query.Append(string.Format(" OR keyword LIKE '%{0}%'", keywords[i]));
        }
    }

    // Add order by
    query.Append(" ORDER BY name");

    using (OleDbCommand command = new OleDbCommand(query.ToString(), sqlcon))
    {
        command.CommandType = CommandType.Text;

        using (OleDbDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                results.Add(reader.GetString(0));
            }
        }
    }

    return results;
}

这篇关于多个搜索关键字的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆