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

所属分类: 网络编程 / ASP.NET 阅读数: 1768
收藏 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 分享

c#  操作符?? null coalescing operator

?? "null coalescing" operator 是c#新提供的一个操作符,这个操作符提供的功能是判断左侧的操作数是否是null,如果是则返回结果是右侧的操作数;非null则返回左侧的操作数。
收藏 0 赞 0 分享

.net 反序题目的详细解答第1/2页

在各种答案,以及平时面试过程中,这道题总归会有一些非常典型的错误发生。其中给老赵的感觉也非常有意思,不知其中的“思路”是否如老赵猜测那样。
收藏 0 赞 0 分享

implicitly convert type 'int' to 'short'的原因与解决方法

implicitly convert type 'int' to 'short'的原因与解决方法
收藏 0 赞 0 分享

比较完整的 asp.net 学习流程

好多朋友想学习后台编程语言,但请注意的事,学习后台是个循序渐进的过程,不可能一下就到位,其实不只是asp.net其它的编程语言都需要下面的一些知识。
收藏 0 赞 0 分享

官网 Ext direct包中.NET版的问题

下载了官网的 Ext direct 包进行研究,发现服务器端返回结果存在一点小问题。
收藏 0 赞 0 分享

C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)第1/2页

C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等),以下就是操作XML的所有方法,相信可以满足很大一部份的使用了。
收藏 0 赞 0 分享

c# 连接字符串数据库服务器端口号 .net状态服务器端口号

正常的数据库连接字符串配置,这是在MSSQL服务器端口是1433(默认)的情况下。
收藏 0 赞 0 分享

ASP.NET 路径问题的解决方法

相对路径和绝对路径在ASP.NET中可以用~/来解决.
收藏 0 赞 0 分享

asp.net TemplateField模板中的Bind方法和Eval方法

在TemplateField模板中为了能够有限制的或者取出数据库中某列的值时,可以用Bind和Eval方法来实现。以下是Bind方法的格式,Eval的格式也是和Bind一样的。 Bind("列的名称","显示的格式文")
收藏 0 赞 0 分享
查看更多