利用字典对象,实现查询条件的动态拼接(抛砖引玉的查询条件自动生成)
假定:
页面有10个输入条件,都是字符类型,并且都是模糊查询(为了方便描述)
每个页面对应数据库的字段是:
A-J
sql.AppendLine("select * from [abc] where 1=1 ");
// 重点看这段
foreach (string key in condition.Keys)
{
if (!string.IsNullOrWhiteSpace(condition[key]))
{
sql.AppendFormat("{0} like '%' + @{0} + '%'", key).AppendLine();
dbHelper.AddInParameter(cmd,key,DbType.String, condition[key]);
}
}