public class SearchModel
{
public string Name { get; set; }//记录数据库字段名
public string Value { get; set; }//记录对应的值
public Action Action { get; set; }//记录相应的操作
}
public enum Action
{
Lessthan,
Greatthan,
Like,
Equart
}
List<SearchModel> ss = new List<SearchModel>();
if (!string.IsNullOrEmpty(Request.Form["txtName"]))//如果用户在名字框中输入了文字
{
SearchModel model = new SearchModel();
model.Name = "BookName";//要操作的字段为书名
model.Value = Request.Form["txtName"];//对应的值为用户输入的文字
model.Action = Action.Like;//操作为like
ss.Add(model);
}//以下类似
if (!string.IsNullOrEmpty(Request.Form["txtAuthor"]))
{
SearchModel model = new SearchModel();
model.Name = "Author";
model.Value = Request.Form["txtAuthor"];
model.Action = Action.Like;
ss.Add(model);
}
if (!string.IsNullOrEmpty(Request.Form["categoryId"]))
{
SearchModel model = new SearchModel();
model.Name = "CategoryId";
model.Value = Request.Form["categoryId"];
model.Action = Action.Equart;
ss.Add(model);
}
if (!string.IsNullOrEmpty(Request.Form["publisherId"]))
{
SearchModel model = new SearchModel();
model.Name = "PublisherId";
model.Value = Request.Form["publisherId"];
model.Action = Action.Equart;
ss.Add(model);
}
if (!string.IsNullOrEmpty(Request.Form["txtISBN"]))
{
SearchModel model = new SearchModel();
model.Name = "ISBN";
model.Value = Request.Form["txtISBN"];
model.Action = Action.Like;
ss.Add(model);
}
if (!string.IsNullOrEmpty(Request.Form["isDiscount"]))
{
SearchModel model = new SearchModel();
model.Name = "Discount";
model.Value = "1";
model.Action = Action.Equart;
ss.Add(model);
}
List<T_Books> books = searchBll.Searc(ss);//这里调用Bll进行操作
public List<T_Books> Search(List<SearchModel> ss)//接收传进来的条件模型类集合,并对其进行遍历
{
string sql = "select * from T_Books where IsDelete=0 and ";//开始拼接sql语句
for (int i = 0; i < ss.Count; i++)
{
if (ss[i].Action == Action.Like)
{
sql += ss[i].Name + " like '%" + ss[i].Value + "%'";
}
if (ss[i].Action == Action.Equart)
{
sql += ss[i].Name + " = " + ss[i].Value;
}
if (ss[i].Action == Action.Greatthan)
{
sql += ss[i].Name + " > " + ss[i].Value;
}
if (ss[i].Action == Action.Lessthan)
{
sql += ss[i].Name + " < " + ss[i].Value;
}
if (i != ss.Count - 1)
{
sql += " and ";
}
}
List<T_Books> list = new List<T_Books>();
DataTable table = SqlHelper.ExecuteDataTable(sql, CommandType.Text);//将拼接好的sql语句传入,开始查询数据库
foreach (DataRow row in table.Rows)
{
T_Books book = GetModelByDataRow.GetBooks(row);
list.Add(book);
}
return list;//返回符合条件的图书集合,完成
select * from T_Books where IsDelete=0 and BookName like '%C++%' and Author like '%JChubby%' and CategoryId = 15 and PublisherId = 16 and ISBN like '11%' and Discount = 1
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有