亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > .NET > 正文

asp.net實現的MVC跨數據庫多表聯合動態條件查詢功能示例

2024-07-10 13:32:02
字體:
來源:轉載
供稿:網友

本文實例講述了asp.net實現的MVC跨數據庫多表聯合動態條件查詢功能。分享給大家供大家參考,具體如下:

一、控制器中方法

[HttpGet]public ActionResult Search(){  ViewBag.HeadTitle = "搜索";  ViewBag.MetaKey = "/"123/"";  ViewBag.MetaDes = "/"456/"";  string whereText = "";  if (Security.HtmlHelper.GetQueryString("first", true) != string.Empty)  {    whereText += " and a.ParentId='" + StringFilter("first", true)+"'";  }  if (Security.HtmlHelper.GetQueryString("second", true) != string.Empty)    whereText += " and a.categoryId='" + StringFilter("second",true)+"'";  string valueStr = "";  if (Security.HtmlHelper.GetQueryString("theme", true) != string.Empty)    valueStr += StringFilter("theme", true) + ",";  if (Security.HtmlHelper.GetQueryString("size", true) != string.Empty)    valueStr += StringFilter("size", true) + ",";  if (Security.HtmlHelper.GetQueryString("font", true) != string.Empty)    valueStr += StringFilter("font", true) + ",";  if (Security.HtmlHelper.GetQueryString("shape", true) != string.Empty)    valueStr += StringFilter("shape", true) + ",";  if (Security.HtmlHelper.GetQueryString("technique", true) != string.Empty)    valueStr += StringFilter("technique", true) + ",";  if (Security.HtmlHelper.GetQueryString("category", true) != string.Empty)    valueStr += StringFilter("category", true) + ",";  if (Security.HtmlHelper.GetQueryString("place", true) != string.Empty)    valueStr += StringFilter("place", true) + ",";  if (Security.HtmlHelper.GetQueryString("price", true) != string.Empty)    valueStr += StringFilter("price", true) + ",";  if (valueStr != "")  {    valueStr=valueStr.Substring(0, valueStr.Length - 1);    whereText += " and f.valueId in("+valueStr+")";  }  if (Security.HtmlHelper.GetQueryString("searchKeys", true) != string.Empty)    whereText += " and a.SaleTitle like '%'" + StringFilter("searchKes", true) + "'%' or a.SaleDes like '%'" + StringFilter("searchKes", true) + "'%' or a.SaleAuthor like '%'" + StringFilter("searchKes", true) + "'%' or a.KeyWords like '%'" + StringFilter("searchKes", true) + "'%' or g.valueProperty like '%'" + StringFilter("searchKes", true) + "'%'";  int pageSize = 50;  int pageIndex = HttpContext.Request.QueryString["pageIndex"].Toint(1);  List<string> searchInfo = Search(pageIndex, pageSize, whereText, 1);  if (Security.HtmlHelper.GetQueryString("sort", true) != string.Empty)  {    string sort = StringFilter("sort", true);    switch (sort)    {      case "1":  //綜合即默認按照上架時間降序排列即按照id降序        searchInfo = Search(pageIndex, pageSize, whereText, 1);        break;      case"2":  //銷量        searchInfo = Search(pageIndex, pageSize, whereText,0, "saleTotal");        break;      case "3":  //收藏        searchInfo = Search(pageIndex, pageSize, whereText,0, "favoritesTotal");        break;      case "4":  //價格升序        searchInfo = Search(pageIndex, pageSize, whereText,1);        break;      case "5":  //價格降序        searchInfo = Search(pageIndex, pageSize, whereText,2);        break;    }  }  string jsonStr = searchInfo[0];  ViewData["jsondata"] = jsonStr;  int allCount = Utility.Toint(searchInfo[1], 0);  ViewBag.AllCount = allCount;  ViewBag.MaxPages = allCount % pageSize == 0 ? allCount / pageSize : (allCount / pageSize + 1).Toint(1);  return View();}[NonAction]public List<string> Search(int pageIndex, int pageSize, string whereText, int orderByPrice, string orderBy = "SaleId"){  BLL.Products searchInfoBLL = new BLL.Products();  List<string> searchInfo = searchInfoBLL.GetSearchInfo(pageIndex, pageSize, whereText, orderByPrice,orderBy);  return searchInfo;}

注:Security.HtmlHelper.GetQueryString(),StringFilter()為自己封裝的方法,用于過濾參數值

二、BLL層方法

using System;using System.Web;using System.Web.Caching;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.Common;using System.Web.Script.Serialization;using FotosayMall.Model;using FotosayMall.Common;using System.Text.RegularExpressions;using System.IO;using Newtonsoft.Json;using Newtonsoft.Json.Converters;using FotosayMall.MVC.Models;namespace FotosayMall.BLL{  public class Products  {    private readonly DAL.Products dal = new DAL.Products();    /// <summary>    /// 分頁查詢,檢索頁數據    /// </summary>    /// <param name="pageIndex"></param>    /// <param name="pageSize"></param>    /// <param name="orderByPrice">價格排序:0默認,1升序,2降序</param>    /// <returns></returns>    public List<string> GetSearchInfo(int pageIndex, int pageSize, string whereText, int orderByPrice, string orderBy = "SaleId")    {      DataSet searchInfoTables = dal.GetSearchInfo(pageIndex, pageSize, whereText);      //總記錄數      int allCount = Utility.Toint(searchInfoTables.Tables[1].Rows[0]["rowsTotal"], 0);      var searchInfo = from list in searchInfoTables.Tables[0].AsEnumerable().OrderByDescending(x => x.Table.Columns[orderBy])        select new SearchModel        {         Url = "/home/products?saleId=" + list.Field<int>("SaleId"),         Author = list.Field<string>("SaleAuthor"),         PhotoFileName = list.Field<string>("PhotoFileName"),         PhotoFilePathFlag = list.Field<int>("PhotoFilePathFlag"),         Province = list.Field<string>("Place").Split(' ').First(),         SalePrice = list.Field<decimal>("SalePrice"),         UsingPrice = list.Field<decimal>("usingPrice"),         Year = list.Field<DateTime>("BuildTime").ToString("yyyy") == "1900" ? "" : list.Field<DateTime>("BuildTime").ToString("yyyy年")        };      if (orderByPrice==2)        searchInfo = searchInfo.OrderByDescending(x => x.Price);      else if (orderByPrice == 1)        searchInfo = searchInfo.OrderBy(x => x.Price);      string jsonStr = JsonConvert.SerializeObject(searchInfo);      List<string> dataList = new List<string>();      dataList.Add(jsonStr);      dataList.Add(allCount.ToString());      return dataList;    }  }}

注:注意觀察由DataTable轉換為可枚舉的可用于Linq查詢的方法方式。

DAL

/// <summary>/// 獲取檢索頁數據/// </summary>/// <param name="pageIndex"></param>/// <param name="pageSize"></param>/// <returns></returns>public DataSet GetSearchInfo(int pageIndex, int pageSize, string whereText){  StringBuilder sqlText = new StringBuilder();  sqlText.Append("select * from (");  sqlText.Append("select a.SaleId,a.PhotoId,SaleTitle,SaleAuthor,a.Status,a.categoryId,c.UserID,c.UserName,b.PhotoFilePathFlag,b.PhotoFileName,coalesce(e.BuildTime,0) BuildTime,c.Place,coalesce(d.usingPrice,0) usingPrice,coalesce(e.SalePrice,0) SalePrice,h.saleTotal,h.favoritesTotal,row_number() over(order by a.saleId) rowsNum ");  sqlText.Append("from fotosay..Photo_Sale a join fotosay..Photo_Basic b on a.PhotoId = b.PhotoID ");  sqlText.Append("join fotosay..System_AccountsDescription c on b.UserID = c.UserID ");  sqlText.Append("left join fotosay..Photo_Sale_Picture d on a.SaleId = d.SaleId ");  sqlText.Append("left join fotosay..Photo_Sale_Tangible e on a.saleId = e.saleId ");  sqlText.Append("join FotosayMall..Fotomall_Product_Relation f on f.saleId = a.SaleId ");  sqlText.Append("join FotosayMall..Fotomall_Product_PropertyValue g on g.categoryId = a.categoryId and g.valueId = f.valueId and g.propertyId = f.propertyId ");  sqlText.Append("join fotosay..Photo_Sale_Property h on a.saleId = h.saleId ");  sqlText.Append("where a.Status=1 " + whereText + " ");  sqlText.Append("group by a.SaleId,a.PhotoId,SaleTitle,SaleAuthor,a.Status,a.categoryId,c.UserID,c.UserName,b.PhotoFilePathFlag,b.PhotoFileName,e.BuildTime,c.Place,usingPrice,SalePrice,h.saleTotal,h.favoritesTotal ");  sqlText.Append(") t where rowsNum between @PageSize*(@PageIndex-1)+1 and @PageSize*@PageIndex;");  sqlText.Append("select count(distinct a.saleId) rowsTotal from fotosay..Photo_Sale a join (select b1.PhotoFilePathFlag,b1.PhotoFileName,b1.UserID,b1.PhotoID from fotosay..Photo_Basic b1 union select b2.PhotoFilePathFlag,b2.PhotoFileName,b2.UserID,b2.PhotoID from fotosay..Photo_Basic_History b2 ) b on a.PhotoId = b.PhotoID join fotosay..System_AccountsDescription c on b.UserID = c.UserID left join fotosay..Photo_Sale_Picture d on a.SaleId = d.SaleId left join fotosay..Photo_Sale_Tangible e on a.saleId = e.saleId join FotosayMall..Fotomall_Product_Relation f on f.saleId = a.SaleId join FotosayMall..Fotomall_Product_PropertyValue g on g.categoryId = a.categoryId and g.valueId = f.valueId and g.propertyId = f.propertyId join fotosay..Photo_Sale_Property h on a.saleId = h.saleId where a.Status=1 " + whereText + ";");  DbParameter[] parameters = {    Fotosay.CreateInDbParameter("@PageIndex", DbType.Int32,pageIndex),    Fotosay.CreateInDbParameter("@PageSize", DbType.Int32,pageSize)    };  DataSet searchInfoList = Fotosay.ExecuteQuery(CommandType.Text, sqlText.ToString(), parameters);  //記錄條數不夠一整頁,則查歷史庫  if (searchInfoList.Tables[0].Rows.Count < pageSize)  {    string sql = "select top(1) a.saleId from fotosay..Photo_Sale a join fotosay..Photo_Basic_History b on a.PhotoId = b.PhotoID join fotosay..System_AccountsDescription c on b.UserID = c.UserID left join fotosay..Photo_Sale_Picture d on a.SaleId = d.SaleId left join fotosay..Photo_Sale_Tangible e on a.saleId = e.saleId join FotosayMall..Fotomall_Product_Relation f on f.saleId = a.SaleId join FotosayMall..Fotomall_Product_PropertyValue g on g.categoryId = a.categoryId and g.valueId = f.valueId and g.propertyId = f.propertyId join fotosay..Photo_Sale_Property h on a.saleId = h.saleId where a.Status=1 " + whereText + ";";    DataSet ds = Fotosay.ExecuteQuery(CommandType.Text, sql.ToString(), parameters);    if (ds != null && ds.Tables[0].Rows.Count > 0)    {      StringBuilder sqlTextMore = new StringBuilder();      sqlTextMore.Append("select * from (");      sqlTextMore.Append("select a.SaleId,a.PhotoId,SaleTitle,SaleAuthor,a.Status,a.categoryId,c.UserID,c.UserName,b.PhotoFilePathFlag,b.PhotoFileName,coalesce(e.BuildTime,0) BuildTime,c.Place,coalesce(d.usingPrice,0) usingPrice,coalesce(e.SalePrice,0) SalePrice,h.saleTotal,h.favoritesTotal,row_number() over(order by a.saleId) rowsNum ");      sqlTextMore.Append("from fotosay..Photo_Sale a ");      sqlTextMore.Append("join (select b1.PhotoFilePathFlag,b1.PhotoFileName,b1.UserID,b1.PhotoID from fotosay..Photo_Basic b1 union select b2.PhotoFilePathFlag,b2.PhotoFileName,b2.UserID,b2.PhotoID from fotosay..Photo_Basic_History b2 ) b on a.PhotoId = b.PhotoID join fotosay..System_AccountsDescription c on b.UserID = c.UserID ");      sqlTextMore.Append("left join fotosay..Photo_Sale_Picture d on a.SaleId = d.SaleId ");      sqlTextMore.Append("left join fotosay..Photo_Sale_Tangible e on a.saleId = e.saleId ");      sqlTextMore.Append("join FotosayMall..Fotomall_Product_Relation f on f.saleId = a.SaleId ");      sqlTextMore.Append("join FotosayMall..Fotomall_Product_PropertyValue g on g.categoryId = a.categoryId and g.valueId = f.valueId and g.propertyId = f.propertyId ");      sqlTextMore.Append("join fotosay..Photo_Sale_Property h on a.saleId = h.saleId ");      sqlTextMore.Append("where a.Status=1 " + whereText + " ");      sqlTextMore.Append("group by a.SaleId,a.PhotoId,SaleTitle,SaleAuthor,a.Status,a.categoryId,c.UserID,c.UserName,b.PhotoFilePathFlag,b.PhotoFileName,e.BuildTime,c.Place,usingPrice,SalePrice,h.saleTotal,h.favoritesTotal");      sqlTextMore.Append(") t where rowsNum between @PageSize*(@PageIndex-1)+1 and @PageSize*@PageIndex;");      sqlTextMore.Append("select count(distinct a.saleId) rowsTotal from fotosay..Photo_Sale a join (select b1.PhotoFilePathFlag,b1.PhotoFileName,b1.UserID,b1.PhotoID from fotosay..Photo_Basic b1 union select b2.PhotoFilePathFlag,b2.PhotoFileName,b2.UserID,b2.PhotoID from fotosay..Photo_Basic_History b2 ) b on a.PhotoId = b.PhotoID join fotosay..System_AccountsDescription c on b.UserID = c.UserID left join fotosay..Photo_Sale_Picture d on a.SaleId = d.SaleId left join fotosay..Photo_Sale_Tangible e on a.saleId = e.saleId join FotosayMall..Fotomall_Product_Relation f on f.saleId = a.SaleId join FotosayMall..Fotomall_Product_PropertyValue g on g.categoryId = a.categoryId and g.valueId = f.valueId and g.propertyId = f.propertyId join fotosay..Photo_Sale_Property h on a.saleId = h.saleId where a.Status=1 " + whereText + ";");      searchInfoList = Fotosay.ExecuteQuery(CommandType.Text, sqlTextMore.ToString(), parameters);    }  }  return searchInfoList;}

注:注意其中使用的跨數據庫查詢的方式和union的一種使用方式

Model

using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Web;namespace FotosayMall.MVC.Models{  public class SearchModel  {    /// <summary>    /// 原始圖片文件夾(用于url地址)    /// </summary>    private const string OriginImagesUrlFolder = "userimages/photos_origin";    /// <summary>    /// 購買頁鏈接    /// </summary>    public string Url { get; set; }    /// <summary>    /// 所屬域名(1為fotosay,2為img,3為img1)    /// </summary>    public int PhotoFilePathFlag { get; set; }    /// <summary>    /// 圖片名稱    /// </summary>    public string PhotoFileName { get; set; }    /// <summary>    /// 商品名稱    /// </summary>    public string Title { get; set; }    /// <summary>    /// 作者所在省份    /// </summary>    public string Province { get; set; }    /// <summary>    /// 作者    /// </summary>    public string Author { get; set; }    /// <summary>    /// 創作年份    /// </summary>    public string Year { get; set; }    /// <summary>    /// 圖片:單次價格    /// </summary>    public decimal UsingPrice { get; set; }    /// <summary>    /// 實物:定價    /// </summary>    public decimal SalePrice { get; set; }    /// <summary>    /// 售價    /// </summary>    public string Price    {      get      {        if (this.UsingPrice > 0)          return this.UsingPrice.ToString();        else if (this.SalePrice > 0)          return this.SalePrice.ToString();        else          return "議價";      }    }    /// <summary>    ///    /// </summary>    private string MasterSite    {      get { return ConfigurationManager.AppSettings["masterSite"].ToString(); }    }    /// <summary>    /// 圖片完整路徑    /// </summary>    public string Img    {      get      {        return MasterSite + "/" + OriginImagesUrlFolder + this.PhotoFileName + "b.jpg";      }    }  }}

希望本文所述對大家asp.net程序設計有所幫助。


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩免费不卡av| 欧美日韩久久久久| 亚洲成人黄色在线| 久久精品视频导航| 亚洲精品久久久久久久久| 中文字幕一区电影| 国产精品扒开腿爽爽爽视频| 一夜七次郎国产精品亚洲| 久久国产精品久久久久久久久久| 国产精品永久免费视频| 国产999精品久久久影片官网| 亚洲专区国产精品| 亚洲影院高清在线| 97久久精品人搡人人玩| 国产精品色婷婷视频| 中文字幕在线精品| 亚洲天堂网在线观看| 韩国美女主播一区| 久久99精品国产99久久6尤物| 97婷婷大伊香蕉精品视频| 亚洲区免费影片| 欧美视频一区二区三区…| 国产99久久精品一区二区永久免费| 国产一区二区三区毛片| 91九色国产在线| 亚洲国产精品高清久久久| 国产精品第100页| 2021久久精品国产99国产精品| 亚洲网站在线观看| 在线观看精品自拍私拍| 日韩视频欧美视频| 国产一区二区三区日韩欧美| 国产成人福利夜色影视| 欧美高清视频在线观看| 丁香五六月婷婷久久激情| 亚洲最大福利视频网| 日韩精品一二三四区| 国产精品久久一区| 久久99热这里只有精品国产| 国产在线一区二区三区| 欧美成人四级hd版| 庆余年2免费日韩剧观看大牛| 91久久精品视频| 日韩69视频在线观看| 日韩中文字幕第一页| 日韩高清中文字幕| 欧美在线视频播放| 欧美第一淫aaasss性| 日韩高清电影好看的电视剧电影| 亚洲一级黄色av| 欧美片一区二区三区| 久久久91精品国产一区不卡| 亚洲精品国产福利| 奇门遁甲1982国语版免费观看高清| 欧美日韩精品在线播放| 久久天天躁日日躁| 中文字幕日韩综合av| 日韩免费在线免费观看| 久久天天躁狠狠躁夜夜爽蜜月| 亚洲综合最新在线| 久久久综合免费视频| 91高清免费在线观看| 精品国内自产拍在线观看| 国产精品视频一区二区三区四| 精品国产福利在线| 97免费中文视频在线观看| 欧美日韩在线视频观看| 欧美一区二区三区免费视| 亚洲精品国产精品国自产观看浪潮| 97色在线播放视频| 亚洲电影免费在线观看| 亚洲的天堂在线中文字幕| 亚洲精品久久久久久久久久久久| 欧美电影免费在线观看| 自拍偷拍免费精品| 久久精品国产久精国产一老狼| 久久久久久久久中文字幕| 国产精品一区二区三区免费视频| 亚洲精品中文字幕女同| 精品久久久久人成| 91精品国产免费久久久久久| 91国偷自产一区二区三区的观看方式| 视频一区视频二区国产精品| 久久九九全国免费精品观看| 成人高清视频观看www| 久久精品一偷一偷国产| 国产精品久久久久久久午夜| 欧美一二三视频| 在线观看亚洲区| 91高清免费视频| 欧美精品性视频| 国产精品老牛影院在线观看| 成人激情春色网| 亚洲香蕉av在线一区二区三区| 有码中文亚洲精品| 日韩精品一区二区三区第95| 91精品国产乱码久久久久久蜜臀| 欧美日韩中文字幕在线视频| 精品久久久91| 北条麻妃一区二区三区中文字幕| 成人亚洲综合色就1024| 亚洲97在线观看| 亚洲国产中文字幕在线观看| 国产精品人成电影在线观看| 91免费电影网站| 精品亚洲一区二区三区四区五区| 亚洲图片制服诱惑| 国产精品一区电影| 91成人福利在线| 国产精品免费看久久久香蕉| 成人日韩在线电影| 国产精品免费视频xxxx| 欧美精品久久久久久久| 欧美精品手机在线| 亚洲精品成人久久久| 日韩精品小视频| 亚洲美女又黄又爽在线观看| 日韩高清电影免费观看完整| 久久理论片午夜琪琪电影网| 欧美成年人视频网站欧美| 欧美第一淫aaasss性| 精品中文字幕在线2019| 91在线精品播放| 亚洲另类激情图| 欧美大片欧美激情性色a∨久久| 久久艹在线视频| 国产欧美日韩综合精品| 黄色一区二区三区| 国内伊人久久久久久网站视频| 久久中文字幕在线| 国产午夜精品麻豆| 日韩av一卡二卡| 亚洲裸体xxxx| 国产69精品久久久久9999| 国产精品久久久久国产a级| 久久久久久久久亚洲| 欧美一级片一区| 色99之美女主播在线视频| 国产视频久久久久久久| 亚洲欧美激情在线视频| 久久这里只有精品视频首页| 38少妇精品导航| 国产成人精品一区二区| 国产精品一区二区av影院萌芽| 日韩精品亚洲视频| 欧美成人精品三级在线观看| 欧美日韩国产页| 日韩av一区在线观看| 国产精品日韩欧美大师| 2019中文字幕全在线观看| 国产成人欧美在线观看| 亚洲综合小说区| 中文字幕日本精品| 久久韩剧网电视剧| 精品在线欧美视频| 精品福利在线观看| 亚洲欧美另类人妖| 欧美性感美女h网站在线观看免费| 青草青草久热精品视频在线网站| 精品成人久久av| 亚洲色图欧美制服丝袜另类第一页| 亚洲欧美精品一区二区| 国产日本欧美一区二区三区| 欧美一区三区三区高中清蜜桃|