欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-15 19:27:59
众所周知,三层将项目分为界面层,业务逻辑层和数据访问层(以最基本的三层为例) 同样都知道,多条件检索其实就是根据用户选择的条件项,然后来拼sql语句 那么,既然要根据用户选择的条件项来拼sql语句,...

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

同样都知道,多条件检索其实就是根据用户选择的条件项,然后来拼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层的sql拼接

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 + "  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;//返回符合条件的图书集合,完成
        }

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

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

最后贴上测试拼接的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
大功告成~!




,>