string strField = "id|className|classAdd";
string strKeyWords = this.tbxKeyWords.Text.Trim();
string strSql = dbexe.searchText("select * from class", strField, strKeyWords);
经常用到多字段的模糊查询,上面的函数可以实现,例如strKeyWords值为“”时,可以输出:
select * from class where id like '%%' or className like '%%' or classAdd like '%%'
函数:
/// <summary>
/// 根据关键字实现多字段模糊查询
/// </summary>
/// <param name="sqlStr">select * from talbe sql语句</param>
/// <param name="sqlText">判断语句条件,是一个用|隔开的字符串</param>
/// <param name="keywords">关键字</param>
public static string searchText(string strSql, string strField, string keywords)
{
StringBuilder sb = new StringBuilder(strSql);
if (strField != string.Empty)
{
sb.Append(" where ");
string[] arrKey = strField.Split('|');
for (int i = 0; i < arrKey.Length; i++)
{
sb.Append(arrKey[i] + " like '%" + keywords + "%' or ");
}
string str = sb.ToString();
//去除最后一个"or"
if (str.IndexOf("or") >= 0)
{
return str.Remove(str.LastIndexOf("or"));
}
return str;
}
return strSql;
}
string strKeyWords = this.tbxKeyWords.Text.Trim();
string strSql = dbexe.searchText("select * from class", strField, strKeyWords);
经常用到多字段的模糊查询,上面的函数可以实现,例如strKeyWords值为“”时,可以输出:
select * from class where id like '%%' or className like '%%' or classAdd like '%%'
函数:
/// <summary>
/// 根据关键字实现多字段模糊查询
/// </summary>
/// <param name="sqlStr">select * from talbe sql语句</param>
/// <param name="sqlText">判断语句条件,是一个用|隔开的字符串</param>
/// <param name="keywords">关键字</param>
public static string searchText(string strSql, string strField, string keywords)
{
StringBuilder sb = new StringBuilder(strSql);
if (strField != string.Empty)
{
sb.Append(" where ");
string[] arrKey = strField.Split('|');
for (int i = 0; i < arrKey.Length; i++)
{
sb.Append(arrKey[i] + " like '%" + keywords + "%' or ");
}
string str = sb.ToString();
//去除最后一个"or"
if (str.IndexOf("or") >= 0)
{
return str.Remove(str.LastIndexOf("or"));
}
return str;
}
return strSql;
}
标签:
asp.net,多字段,模糊查询
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“asp.net 多字段模糊查询代码”评论...