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

所属分类: 网络编程 / ASP.NET 阅读数: 1810
收藏 0 赞 0 分享

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

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

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

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

那么还架三层做什么?

那我在数据访问层拼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;//返回符合条件的图书集合,完成

 假设用户输入下图的条件:

最后贴上测试拼接的sql语句,如下

select * from T_Books where IsDelete=0 and BookName like '%C++%' and Author like '%JChubby%' and CategoryId = 15 and PublisherId = 16 and ISBN like '%1111%' and Discount = 1
更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多