asp.net多关键词搜索函数

private string search_about(string keywords, string fileds)

    {
     
  string[] words ={ };
      
  string sql_temp = "", sql = "";

        if (keywords != "")

        {

            words = keywords.Split(' ');

            for (int i = 0; i < words.Length; i++)

            {

                int first = 0;

                sql_temp = "( ";

               

                    if (first == 0)

                    {

                        sql_temp += fileds + " like '%" + words[i] + "%'";

                        first = 1;

                    }

                    else

                    {

                        sql_temp += " AND " + fileds + " like '%" + words[i] + "%'"; ;

                    }
                if (sql_temp.Trim() != "(") sql_temp += " ) AND ";

                if (i == 0) sql = sql_temp;

                   else

                   {

                    sql += sql_temp;

                   }

            }

            sql = sql.Substring(0, sql.LastIndexOf("AND"));

        }

        return sql;

    }

posted on 2008-07-16 16:36  草原和大树  阅读(637)  评论(0编辑  收藏  举报