asp.net用三層實現多條件檢索樣本_實用技巧

來源:互聯網
上載者:User

眾所周知,三層將項目分為介面層,商務邏輯層和資料訪問層(以最基本的三層為例)

同樣都知道,多條件檢索其實就是根據使用者選擇的條件項,然後來拼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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.