• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

asp 搞代码 4年前 (2022-01-03) 31次浏览 已收录 0个评论

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

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

同样都知道,多条件检索其实就是根据用户选择的条件项,然后来拼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 ss = new List(); 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 books = searchBll.Searc(ss);//这里调用Bll进行操作

Bll就先不说,主要是Dal层的s

来源gaodai.ma#com搞##代!^码网

ql拼接

 public List Search(List ss)//接收传进来的条件模型类集合,并对其进行遍历 { string sql = "select * from T_Books where IsDelete=0 and ";//开始拼接sql语句 for (int i = 0; i  " + ss[i].Value; } if (ss[i].Action == Action.Lessthan) { sql += ss[i].Name + " <" + ss[i].Value; } if (i != ss.Count - 1) { sql += " and "; } } List list = new List(); 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用三层实现多条件检索示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:asp.net用三层实现多条件检索示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址