您的位置:首页 > 编程语言 > ASP

asp.net用三层实现多条件检索示例

2014-07-11 15:41 489 查看
三层将项目分为界面层,业务逻辑层和数据访问层,下面为大家介绍下asp.net如何用三层实现多条件检索,感兴趣的朋友可以参考下

众所周知,三层将项目分为界面www.avcang.com层,业务逻辑层和数据访问层(以最基本的三层为例)

同样都知道,多条件检索其实就是根据用户选择www.wnsr668.com的条件项,然后来拼sql语句

那么,既然要根据用户选择的条件项来拼sql语句,就肯定要在界面层接收用户的选择,这时候问题来了:

我是要在界面层拼sql语句吗,这么做完全没问题,功能也完www.sepeizi.com全可以实现,可是这么一来,你是破坏了三层的原则了吗

那么还架三层做什么?

那我在数据访问层拼sql语句好了,然后问题又来了:

在数据访问层拼的话这么知道用户选择了哪几个条件项呢,根据分层的原则,是不能把诸如textBox1.Text这样的数据传给数据访问层的

其实解决的方案就是第二种方式,只是中间通过一个条件模型类来传递用户的选择

条件模型类如下:

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 

}对应数据中中的几个操作,如<,>,like,=等,可以根据自己的需要添加

当然你也可以用数字,不过魔鬼数字最好不要使用,所以还是定义一个枚举吧~动动手指头就ok了

假设现在要对一个图书表进行多条件检索

在界面层中的代码:

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进行操作Bll就先不说,主要是Dal层的sql拼接

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;//返回符合条件的图书集合,完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息