problem with multiple search keywords

痴心易碎 提交于 2019-12-14 03:34:40

问题


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; 

回答1:


You are executing a new SELECT statement for each item in the keyword list. Instead, try building the where clause and then executing the select statement:

public override list<result> retrunsearch(string search)
{
  string[] search = pQuery.Split(',');
  List <result> myresult = new List<result>();

  // Build WHERE
  for (int i = 1; i < search.Length; i++)
    where += " And '%" + search[i] + "%'";

  // Now search
  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);
    result.add(result1);
  }
  sdaResult0.Close();

  return result;
}

A couple of quick notes:

  • I'm lazy, so I preserved errors in your code such as not declaring the "where" variable.
  • You might need to use "OR" instead of "AND" in your WHERE clause, depending on how you want your search to work.
  • The approach you are taking is subject to a SQL injection attack.



回答2:


You need to refactor your method a bit. Only the query appending should be in the for loop:

public override list<result> retrunsearch(string search)
{
    string[] search = pQuery.Split(',');
    List <result> myresult = new List<result>();

    OleDbCommand cmd = new OleDbCommand("select Distinct name from table1 where search like '%" + search[0] + "%', sqlcon);
    cmd.CommandType = CommandType.Text;
    for (int i = 1; i < search.Length; i++)
    {
        cmd.CommandText += " AND search like '%" + search[i] + "%'";
    }
    cmd.CommandText += " order by name";

    OleDbDataReader sdaResult0 = cmd.ExecuteReader();
    while (sdaResult0.Read())
    {
        result restult1= new result();
        result1.name   = sdaResult0.String(0);
        myresult.add(result1);
    }

    sdaResult0.Close();
    return myresult;
}



回答3:


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;
}


来源:https://stackoverflow.com/questions/5790123/problem-with-multiple-search-keywords

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!