#region 组装所有分段数据放在一个大的对象里面 public DataTable RestProductList(DataTable dtProduct, LineEntity model) { if (dtProduct != null && dtProduct.Rows.Count > 0) { if (!dtProduct.Columns.Contains("PrepayMent")) //销售额 { dtProduct.Columns.Add("PrepayMent"); } if (!dtProduct.Columns.Contains("Profit"))//成本 { dtProduct.Columns.Add("Profit"); } if (!dtProduct.Columns.Contains("lirun"))//利润 { dtProduct.Columns.Add("lirun"); } if (!dtProduct.Columns.Contains("liruilv"))//利润率 { dtProduct.Columns.Add("liruilv"); } if (!dtProduct.Columns.Contains("yxDan"))//意向单 { dtProduct.Columns.Add("yxDan"); } if (!dtProduct.Columns.Contains("onlineDan"))//online订单 { dtProduct.Columns.Add("onlineDan"); } if (!dtProduct.Columns.Contains("successDan"))//成功单 { dtProduct.Columns.Add("successDan"); } if (!dtProduct.Columns.Contains("tripPeople"))//出行人数 { dtProduct.Columns.Add("tripPeople"); } if (!dtProduct.Columns.Contains("successLv"))//成功转换率 { dtProduct.Columns.Add("successLv"); } if (!dtProduct.Columns.Contains("alluv"))//产品页面流量 { dtProduct.Columns.Add("alluv"); } if (!dtProduct.Columns.Contains("yudianlv"))//预定浏览比 { dtProduct.Columns.Add("yudianlv"); } if (!dtProduct.Columns.Contains("onlinelv"))//在线预定率 { dtProduct.Columns.Add("onlinelv"); } if (!dtProduct.Columns.Contains("HuiFang"))//点评 { dtProduct.Columns.Add("HuiFang"); } if (!dtProduct.Columns.Contains("Satisfaction"))//满意度 { dtProduct.Columns.Add("Satisfaction"); } StringBuilder sbIds = new StringBuilder(); foreach (DataRow dr in dtProduct.Rows) { sbIds.Append(Convert.ToString(dr["ID"]) + ","); } if (sbIds.Length > 0) { DataTable dtOrder = null;//每页订单数据 //DataTable dtOrderYxCount = null;//每页意向订单总数 //DataTable dtOrderOnlineCount = null;//每页online订单 //DataTable dtSuccessCount = null;//每页成功单 DataTable dtOrderTypeCount = null;//每页订单不同类型的总数 DataTable dtProUv = null;//每页UV数据 DataTable dtTempProCount = null;//每页满意度点评 sbIds.Remove(sbIds.Length - 1, 1); dtOrder = GetOrderData(sbIds.ToString(), model);//取得一页订单,相关数据 //dtOrderYxCount = GetOrderPurposeCountByIds(0, -999, sbIds.ToString(),model);//意向单数据集 //dtOrderOnlineCount = GetOrderPurposeCountByIds(0, 38, sbIds.ToString(),model);//online订单数据集 //dtSuccessCount = GetOrderPurposeCountByIds(7, -999, sbIds.ToString(),model);//成功单数据集 dtOrderTypeCount = GetNewOrderPurposeCountByIds(sbIds.ToString(), model);//每页订单不同类型的总数 dtProUv = GetProductUVCountByIds(sbIds.ToString(), model);//UV数据集 dtTempProCount = GetNewTempProductByIds(sbIds.ToString());//满意度点评数据集 foreach (DataRow dr in dtProduct.Rows) { #region 变量属性 int pid = Convert.ToInt32(dr["id"]);//产品编号 string[] OrderList = GetOrderById(dtOrder, pid); double prepayMent = Convert.ToDouble(OrderList[0]);//销售额 double profit = Convert.ToDouble(OrderList[1]);//成本 double lirun = prepayMent - profit; //利润 double liruilv = prepayMent == 0 ? 0 : Math.Round(lirun / prepayMent * 100, 2);//利润率 int[] OrderTypeCount = GetOrderNumCountById(dtOrderTypeCount, pid); int yxDan = OrderTypeCount[0];//意向单 int onlineDan = OrderTypeCount[1];//online订单 int successDan = OrderTypeCount[2];//成功单 double successLv = yxDan == 0 ? 0 : Math.Round(Convert.ToDouble(successDan) / yxDan * 100, 2);//成功转换率 int alluv = GetProUvCountById(dtProUv, pid);//产品页面流量 double yudianlv = alluv == 0 ? 0 : Math.Round(Convert.ToDouble(yxDan) / alluv * 100, 2); //预定浏览比 double onlinelv = yxDan == 0 ? 0 : Math.Round(Convert.ToDouble(onlineDan) / yxDan * 100, 2); //在线预定率 double[] tempProList = GetNewTempProCountById(dtTempProCount, pid); double Satisfaction = (int)(tempProList[0] * 100);//满意度 double HuiFang = (int)tempProList[1];//点评 #endregion dr["PrepayMent"] = prepayMent; dr["Profit"] = profit; dr["lirun"] = lirun; dr["liruilv"] = liruilv; dr["tripPeople"] = Convert.ToInt32(OrderList[2]) + Convert.ToInt32(OrderList[3]); dr["yxDan"] = yxDan; dr["onlineDan"] = onlineDan; dr["successDan"] = successDan; dr["successLv"] = successLv; dr["alluv"] = alluv; dr["yudianlv"] = yudianlv; dr["onlinelv"] = onlinelv; dr["HuiFang"] = HuiFang; dr["Satisfaction"] = Satisfaction + "%"; } return dtProduct; } } return null; } #endregion
表格 每页 单独 是 这样查 select * from 表 where id in(一组编号)
如果 页长 是 10 , id 里面的 数据 如 : 1001,1002,1003...,1010
为什么这样写 减少连接数据库次数。
再拿 分段数据 搜索你要展示的数据
可以考虑用DataTable对象select方法,搜索具体某个产品编号对应的产品信息 DataRow[] arrDr = dt.Select("Productid=" + id);
好处:减少数据库压力。
/// <summary> /// 通过pid得到订单区域,数据源为预先查好的DataTable /// </summary> /// <param name="id">产品id</param> /// <returns></returns> public string[] GetOrderById(DataTable dt, int id) { string[] result = new string[4]; for (int i = 0; i < result.Length; i++) { result[i] = "0"; } if (dt != null && dt.Rows.Count > 0) { DataRow[] arrDr = dt.Select("ProductID=" + id); if (arrDr != null && arrDr.Length > 0) { result[0] = Convert.ToString(arrDr[0]["PrepayMent"]); result[1] = Convert.ToString(arrDr[0]["Profit"]); result[2] = Convert.ToString(arrDr[0]["Person"]); result[3] = Convert.ToString(arrDr[0]["Child"]); } } return result; }
/// <summary> /// 根据产品id集合查询 意向和online订单数和成功单 (总数) /// </summary> /// <returns></returns> public DataTable GetNewOrderPurposeCountByIds(string pids, LineEntity model) { StringBuilder sb = new StringBuilder(); if (model.StartTime != "" && model.EndTime != "") sb.AppendFormat(" and joindate between '{0}' and '{1}'", model.StartTime, Convert.ToDateTime(model.EndTime).AddHours(23).AddMinutes(59).AddSeconds(59).ToString()); string sql = string.Format(@" SELECT PRODUCTID,COUNT(CASE WHEN IsSupplierReturn = 0 THEN 1 END) ORDERCOUNT, count(CASE WHEN IsSupplierReturn =0 AND firstname='客户' THEN 1 END ) as onlineCount, COUNT(CASE WHEN IsSupplierReturn =0 AND ENABLE IN (7,8) THEN 1 END) AS SUCCEEDCOUNT FROM view0010(nolock) WHERE ProductID in({0}) {1} GROUP BY PRODUCT ID ", pids, sb.ToString()); DataTable table = SQLHelper.SqlHelper.ExecuteDataTable(sqlcon, CommandType.Text, sql); return table; }
好处:这种处理方法 适合 数据量比较大 ,关联的表比较多,在性能方面有很大的提升。
我的方式: 主表里面 只有3个字段可用 select a,b,c from 表 .
其它的 e,d,f,....列用 组装 分段数据来实现