asp.net用三層實現(xiàn)多條件檢索示例
眾所周知,三層將項目分為界面層,業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層(以最基本的三層為例)
同樣都知道,多條件檢索其實就是根據(jù)用戶選擇的條件項,然后來拼sql語句
那么,既然要根據(jù)用戶選擇的條件項來拼sql語句,就肯定要在界面層接收用戶的選擇,這時候問題來了:
我是要在界面層拼sql語句嗎,這么做完全沒問題,功能也完全可以實現(xiàn),可是這么一來,你是破壞了三層的原則了嗎
那么還架三層做什么?
那我在數(shù)據(jù)訪問層拼sql語句好了,然后問題又來了:
在數(shù)據(jù)訪問層拼的話這么知道用戶選擇了哪幾個條件項呢,根據(jù)分層的原則,是不能把諸如textBox1.Text這樣的數(shù)據(jù)傳給數(shù)據(jù)訪問層的
其實解決的方案就是第二種方式,只是中間通過一個條件模型類來傳遞用戶的選擇
條件模型類如下:
public class SearchModel { public string Name { get; set; }//記錄數(shù)據(jù)庫字段名 public string Value { get; set; }//記錄對應(yīng)的值 public Action Action { get; set; }//記錄相應(yīng)的操作 }
選擇很難看出這個類的作用到底是什么,接著走你~
之后要準(zhǔn)備一個枚舉:
public enum Action { Lessthan, Greatthan, Like, Equart }
對應(yīng)數(shù)據(jù)中中的幾個操作,如<,>,like,=等,可以根據(jù)自己的需要添加
當(dāng)然你也可以用數(shù)字,不過魔鬼數(shù)字最好不要使用,所以還是定義一個枚舉吧~動動手指頭就ok了
假設(shè)現(xiàn)在要對一個圖書表進行多條件檢索
在界面層中的代碼:
List<SearchModel> ss = new List<SearchModel>(); if (!string.IsNullOrEmpty(Request.Form["txtName"]))//如果用戶在名字框中輸入了文字 { SearchModel model = new SearchModel(); model.Name = "BookName";//要操作的字段為書名 model.Value = Request.Form["txtName"];//對應(yīng)的值為用戶輸入的文字 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);//這里調(diào)用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語句傳入,開始查詢數(shù)據(jù)庫 foreach (DataRow row in table.Rows) { T_Books book = GetModelByDataRow.GetBooks(row); list.Add(book); } return list;//返回符合條件的圖書集合,完成
假設(shè)用戶輸入下圖的條件:
最后貼上測試拼接的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
相關(guān)文章
asp.net實現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表的方法
這篇文章主要介紹了asp.net實現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表的方法,涉及asp.net基于DataTable的數(shù)據(jù)庫及excel操作相關(guān)技巧,需要的朋友可以參考下2015-12-12ASP.NET Core 奇淫技巧之偽屬性注入的實現(xiàn)
這篇文章主要介紹了ASP.NET Core 奇淫技巧之偽屬性注入的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08.NET讀寫Excel工具Spire.Xls使用 Excel單元格控制(3)
這篇文章主要為大家詳細介紹了.NET讀寫Excel工具Spire.Xls使用,Excel單元格控制,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11jquery repeater 模仿 Google 展開頁面預(yù)覽子視圖
節(jié)后的這一周, 希望大家能挺住, hehe, 這兩天給大家準(zhǔn)備一個 Repeater 子視圖的例子, 模擬了 Google 搜索結(jié)果后的頁面的預(yù)覽, 其實也只是顯示了一段問題2011-10-10asp.net 繼承自Page實現(xiàn)統(tǒng)一頁面驗證與錯誤處理
一直以來,我都在思考以前一個項目中,后臺文件中很多的.aspx文件上的權(quán)限判斷問題,傻乎乎的我基本上每個文件當(dāng)時都給加了一句2009-04-04