¡¡¡¡ÔÚ½çÃæ²ãÖеĴúÂ룺
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
¡¡¡¡´ó¹¦¸æ³É~£¡