个人资料

跳过导航链接首页 > 博客列表 > 博客正文

C#使用sql语句进行分词模糊搜索

分类:

C#使用sql语句进行分词模糊搜索,首先将搜索语句用空格分割,获得搜索词数组keywords,然后对keywords数组内每个元素分别进行sql查询,组合sql语句

string[] keywords = SearchInput.Value.Split(' ');
string searchCmd = "SELECT * FROM [blog]";
for (int i = 0; i < keywords.Count(); i++)
{
    if (i == 0)
    {
        searchCmd += "where [topic] like '%" + keywords[i] + "%'";
        searchCmd += "or [Title] like '%" + keywords[i] + "%'";
        searchCmd += "or [userName] like '%" + keywords[i] + "%'";
    }
    else
    {
        searchCmd += "or [topic] like '%" + keywords[i] + "%'";
        searchCmd += "or [Title] like '%" + keywords[i] + "%'";
        searchCmd += "or [userName] like '%" + keywords[i] + "%'";
    }
 
}
 
searchCmd += "ORDER BY [Time] DESC";
AccessDataSource1.SelectCommand = searchCmd;

end~

songshizhao
最初发表2017/10/1 17:40:10 最近更新2017/10/1 17:40:10 5502
为此篇作品打分
10
   评论