asp.net用三層實(shí)現(xiàn)多條件檢索示例

字號(hào):


    三層將項(xiàng)目分為界面層,業(yè)務(wù)邏輯層和數(shù)據(jù)訪問(wèn)層,下面為大家介紹下asp.net如何用三層實(shí)現(xiàn)多條件檢索,感興趣的朋友可以參考下。
    眾所周知,三層將項(xiàng)目分為界面層,業(yè)務(wù)邏輯層和數(shù)據(jù)訪問(wèn)層(以最基本的三層為例)
    同樣都知道,多條件檢索其實(shí)就是根據(jù)用戶(hù)選擇的條件項(xiàng),然后來(lái)拼sql語(yǔ)句
    那么,既然要根據(jù)用戶(hù)選擇的條件項(xiàng)來(lái)拼sql語(yǔ)句,就肯定要在界面層接收用戶(hù)的選擇,這時(shí)候問(wèn)題來(lái)了:
    我是要在界面層拼sql語(yǔ)句嗎,這么做完全沒(méi)問(wèn)題,功能也完全可以實(shí)現(xiàn),可是這么一來(lái),你是破壞了三層的原則了嗎
    那么還架三層做什么?
    那我在數(shù)據(jù)訪問(wèn)層拼sql語(yǔ)句好了,然后問(wèn)題又來(lái)了:
    在數(shù)據(jù)訪問(wèn)層拼的話這么知道用戶(hù)選擇了哪幾個(gè)條件項(xiàng)呢,根據(jù)分層的原則,是不能把諸如textBox1.Text這樣的數(shù)據(jù)傳給數(shù)據(jù)訪問(wèn)層的
    其實(shí)解決的方案就是第二種方式,只是中間通過(guò)一個(gè)條件模型類(lèi)來(lái)傳遞用戶(hù)的選擇
    條件模型類(lèi)如下:
    public class SearchModel
    {
    public string Name { get; set; }//記錄數(shù)據(jù)庫(kù)字段名
    public string Value { get; set; }//記錄對(duì)應(yīng)的值
    public Action Action { get; set; }//記錄相應(yīng)的操作
    }選擇很難看出這個(gè)類(lèi)的作用到底是什么,接著走你~
    之后要準(zhǔn)備一個(gè)枚舉:
    public enum Action
    {
    Lessthan,
    Greatthan,
    Like,
    Equart
    }對(duì)應(yīng)數(shù)據(jù)中中的幾個(gè)操作,如<,>,like,=等,可以根據(jù)自己的需要添加
    當(dāng)然你也可以用數(shù)字,不過(guò)魔鬼數(shù)字最好不要使用,所以還是定義一個(gè)枚舉吧~動(dòng)動(dòng)手指頭就ok了
    假設(shè)現(xiàn)在要對(duì)一個(gè)圖書(shū)表進(jìn)行多條件檢索
    在界面層中的代碼:
    List<SearchModel> ss = new List<SearchModel>();
    if (!string.IsNullOrEmpty(Request.Form["txtName"]))//如果用戶(hù)在名字框中輸入了文字
    {
    SearchModel model = new SearchModel();
    model.Name = "BookName";//要操作的字段為書(shū)名
    model.Value = Request.Form["txtName"];//對(duì)應(yīng)的值為用戶(hù)輸入的文字
    model.Action = Action.Like;//操作為like
    ss.Add(model);
    }//以下類(lèi)似
    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);//這里調(diào)用Bll進(jìn)行操作Bll就先不說(shuō),主要是Dal層的sql拼接
    public List<T_Books> Search(List<SearchModel> ss)//接收傳進(jìn)來(lái)的條件模型類(lèi)集合,并對(duì)其進(jìn)行遍歷
    {
    string sql = "select * from T_Books where IsDelete=0 and ";//開(kāi)始拼接sql語(yǔ)句
    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語(yǔ)句傳入,開(kāi)始查詢(xún)數(shù)據(jù)庫(kù)
    foreach (DataRow row in table.Rows)
    {
    T_Books book = GetModelByDataRow.GetBooks(row);
    list.Add(book);
    }
    return list;//返回符合條件的圖書(shū)集合,完成