asp.net 多字段模糊查詢代碼
來源:懂視網(wǎng)
責編:小采
時間:2020-11-27 22:45:08
asp.net 多字段模糊查詢代碼
asp.net 多字段模糊查詢代碼:string strField = id|className|classAdd; string strKeyWords = this.tbxKeyWords.Text.Trim(); string strSql = dbexe.searchText(select * from class, strField, strKeyWords); 經常用到多字段的模糊查詢,上面的函數(shù)可以實現(xiàn),例如strKeyWords值為腳本之家時,可以輸出: select * from
導讀asp.net 多字段模糊查詢代碼:string strField = id|className|classAdd; string strKeyWords = this.tbxKeyWords.Text.Trim(); string strSql = dbexe.searchText(select * from class, strField, strKeyWords); 經常用到多字段的模糊查詢,上面的函數(shù)可以實現(xiàn),例如strKeyWords值為腳本之家時,可以輸出: select * from
string strField = "id|className|classAdd";
string strKeyWords = this.tbxKeyWords.Text.Trim();
string strSql = dbexe.searchText("select * from class", strField, strKeyWords);
經常用到多字段的模糊查詢,上面的函數(shù)可以實現(xiàn),例如strKeyWords值為“腳本之家”時,可以輸出:
select * from class where id like '%腳本之家%' or className like '%腳本之家%' or classAdd like '%腳本之家%'
函數(shù):
/// <summary>
/// 根據(jù)關鍵字實現(xiàn)多字段模糊查詢
/// </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;
}
聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
asp.net 多字段模糊查詢代碼
asp.net 多字段模糊查詢代碼:string strField = id|className|classAdd; string strKeyWords = this.tbxKeyWords.Text.Trim(); string strSql = dbexe.searchText(select * from class, strField, strKeyWords); 經常用到多字段的模糊查詢,上面的函數(shù)可以實現(xiàn),例如strKeyWords值為腳本之家時,可以輸出: select * from