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~