婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫(kù) > asp.net用三層實(shí)現(xiàn)多條件檢索示例

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

熱門標(biāo)簽:征途美甲店地圖標(biāo)注 昆明語音電銷機(jī)器人價(jià)格 浦發(fā)電話機(jī)器人提醒還款 百度地圖怎樣做地圖標(biāo)注 騰訊地圖標(biāo)注手機(jī) 電銷語音機(jī)器人型號(hào)參數(shù) 400電話如何申請(qǐng)取消 太原400電話上門辦理 柳州電銷機(jī)器人公司

眾所周知,三層將項(xiàng)目分為界面層,業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層(以最基本的三層為例)

同樣都知道,多條件檢索其實(shí)就是根據(jù)用戶選擇的條件項(xiàng),然后來拼sql語句

那么,既然要根據(jù)用戶選擇的條件項(xiàng)來拼sql語句,就肯定要在界面層接收用戶的選擇,這時(shí)候問題來了:

我是要在界面層拼sql語句嗎,這么做完全沒問題,功能也完全可以實(shí)現(xiàn),可是這么一來,你是破壞了三層的原則了嗎

那么還架三層做什么?

那我在數(shù)據(jù)訪問層拼sql語句好了,然后問題又來了:

在數(shù)據(jù)訪問層拼的話這么知道用戶選擇了哪幾個(gè)條件項(xiàng)呢,根據(jù)分層的原則,是不能把諸如textBox1.Text這樣的數(shù)據(jù)傳給數(shù)據(jù)訪問層的

其實(shí)解決的方案就是第二種方式,只是中間通過一個(gè)條件模型類來傳遞用戶的選擇

條件模型類如下:

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è)類的作用到底是什么,接著走你~

之后要準(zhǔn)備一個(gè)枚舉:

public enum Action 
{ 
Lessthan, 
Greatthan, 
Like, 
Equart 
}

對(duì)應(yīng)數(shù)據(jù)中中的幾個(gè)操作,如,>,like,=等,可以根據(jù)自己的需要添加

當(dāng)然你也可以用數(shù)字,不過魔鬼數(shù)字最好不要使用,所以還是定義一個(gè)枚舉吧~動(dòng)動(dòng)手指頭就ok了

假設(shè)現(xiàn)在要對(duì)一個(gè)圖書表進(jìn)行多條件檢索

在界面層中的代碼:

ListSearchModel> ss = new ListSearchModel>(); 
if (!string.IsNullOrEmpty(Request.Form["txtName"]))//如果用戶在名字框中輸入了文字 
{ 
SearchModel model = new SearchModel(); 
model.Name = "BookName";//要操作的字段為書名 
model.Value = Request.Form["txtName"];//對(duì)應(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); 
} 
ListT_Books> books = searchBll.Searc(ss);//這里調(diào)用Bll進(jìn)行操作

Bll就先不說,主要是Dal層的sql拼接

public ListT_Books> Search(ListSearchModel> ss)//接收傳進(jìn)來的條件模型類集合,并對(duì)其進(jìn)行遍歷 
{ 
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 "; 
} 
} 
ListT_Books> list = new ListT_Books>(); 
DataTable table = SqlHelper.ExecuteDataTable(sql, CommandType.Text);//將拼接好的sql語句傳入,開始查詢數(shù)據(jù)庫(kù) 
foreach (DataRow row in table.Rows) 
{ 
T_Books book = GetModelByDataRow.GetBooks(row); 
list.Add(book); 
} 
return list;//返回符合條件的圖書集合,完成

 假設(shè)用戶輸入下圖的條件:

最后貼上測(cè)試拼接的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 在三層架構(gòu)中事務(wù)的使用實(shí)例代碼
  • 擴(kuò)展ASP.NET MVC三層框架且使用StructureMap實(shí)現(xiàn)依賴注入1-Model層
  • asp.net實(shí)現(xiàn)三層架構(gòu)的例子
  • ASP.NET MVC5 網(wǎng)站開發(fā)框架模型、數(shù)據(jù)存儲(chǔ)、業(yè)務(wù)邏輯(三)
  • ASP.NET創(chuàng)建三層架構(gòu)圖解詳細(xì)教程
  • 在ASP.NET 2.0中操作數(shù)據(jù)之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層
  • 在ASP.NET 2.0中操作數(shù)據(jù)之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

標(biāo)簽:新疆 天門 張家界 江蘇 白山 德陽 蘭州 陽泉

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net用三層實(shí)現(xiàn)多條件檢索示例》,本文關(guān)鍵詞  asp.net,用,三層,實(shí)現(xiàn),多,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net用三層實(shí)現(xiàn)多條件檢索示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net用三層實(shí)現(xiàn)多條件檢索示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 漠河县| 鞍山市| 安塞县| 那曲县| 大荔县| 巫山县| 浙江省| 府谷县| 通辽市| 余姚市| 万全县| 武川县| 弥勒县| 洮南市| 涞水县| 温州市| 米易县| 邻水| 台东市| 和林格尔县| 东源县| 喀喇| 金沙县| 密云县| 庆元县| 衡阳市| 巫山县| 福建省| 临海市| 环江| 余庆县| 墨江| 华池县| 驻马店市| 晋江市| 潮安县| 中江县| 延安市| 高台县| 塘沽区| 清苑县|