服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net用三层实现多条件检索示例

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

2019-12-06 12:41whsnow ASP.NET教程

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

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

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

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

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

那么还架三层做什么?

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

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

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

条件模型类如下:

?
1
2
3
4
5
6
public class SearchModel
{
public string Name { get; set; }//记录数据库字段名
public string Value { get; set; }//记录对应的值
public Action Action { get; set; }//记录相应的操作
}

选择很难看出这个类的作用到底是什么,接着走你~

之后要准备一个枚举:

?
1
2
3
4
5
6
7
public enum Action
{
Lessthan,
Greatthan,
Like,
Equart
}

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

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

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

在界面层中的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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拼接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;//返回符合条件的图书集合,完成

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

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

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

?
1
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

 

延伸 · 阅读

精彩推荐