asp.net关键字模糊查询(类似于各大网站搜索功能)

  今天与大家分享一个小算法,我是用C#写的,其他语言也一样换下语法就行了。以空格断开关键字模糊查询,类似于各大网站的搜索功能。我想的就是一个拼接sql语句的过程。都是以空格断开关键字,分两种情况,一种是以空格断开是”且”的关系,另一种是以空格断开是”或”的关系。其实也很简单,直接上代码吧。

  关键字"或"的关系代码:

/// <summary>
    /// 关键字"或"的关系
    /// </summary>
    /// <returns></returns>
    public string OrKeyWords()
    {
        string sql = "select username from usertable where 1=1";
        string txtkeyword = txttitle.Text.Trim();
        if (txtkeyword == "")
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请输入关键字!')", true);
            return;
        }
        else
        {            
            int len = txtkeyword.Length;
            int n = 0;
            string strsql = " and (";
            string strkey = "";
            for (int i = 0; i < len; i++)
            {
                if (txtkeyword[i].ToString() == " ")
                {
                    if (i > 0)
                    {
                        if (txtkeyword[i - 1].ToString() != " ")
                        {
                            n++;
                            strsql += "charindex('" + strkey + "',username)>0 or ";
                            strkey = "";
                        }
                    }
                }
                else
                {
                    strkey += txtkeyword[i].ToString();
                }
            }
            if (n == 0)
            {
                sql += " and charindex('" + txtkeyword + "',username)>0 order by id desc";
            }
            else
            {
                strsql += "charindex('" + strkey + "',username)>0)";
                sql += "" + strsql + " order by id desc";
            }
            //拼接完成
            return sql;
        }
    }

  关键字"且"的关系代码:

/// <summary>
    /// 关键字"且"的关系
    /// </summary>
    /// <returns></returns>
    public string AndKeyWords()
    {
        string sql = "select username from usertable where 1=1";
        string txtkeyword = txttitle.Text.Trim();
        if (ttxtkeyword == "")
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请输入关键字!')", true);
            return;
        }
        else
        {            
            int len = txtkeyword.Length;
            int n = 0;
            string strsql = " and ";
            string strkey = "";
            for (int i = 0; i < len; i++)
            {
                if (txtkeyword[i].ToString() == " ")
                {
                    if (i > 0)
                    {
                        if (txtkeyword[i - 1].ToString() != " ")
                        {
                            n++;
                            strsql += "charindex('" + strkey + "',username)>0 and ";
                            strkey = "";
                        }
                    }
                }
                else
                {
                    strkey += txtkeyword[i].ToString();
                }
            }
            if (n == 0)
            {
                sql += " and charindex('" + txtkeyword + "',username)>0 order by id desc";
            }
            else
            {
                strsql += "charindex('" + strkey + "',username)>0";
                sql += "" + strsql + " order by id desc";
            }
            //拼接完成
            return sql;
        }
    }

  代码很简单,就不解释了哈,我自己记录一下,也希望对大家有帮助。

  PS:Tandy Tang祝大家写代码写的愉快!

posted @ 2012-08-30 09:59  大神神风  阅读(2341)  评论(0编辑  收藏  举报