這段時間作個項目,數據量大的時候分頁效果不是不多,用存儲過程優化了一下,跟菜鳥們分享一下 共同進步。html
存儲過程:sql
- set ANSI_NULLS ON
- set QUOTED_IDENTIFIER ON
- go
- /*分頁存儲過程
- Descript:分頁存儲過程
- Author:Blue.Dream
- Date:2004-8-18 21:01
- */
- ALTER PROCEDURE [dbo].[ListPage](
- @tblName nvarchar(200), ----要顯示的表或多個表的鏈接
- @fldName nvarchar(200) = '*', ----要顯示的字段列表
- @pageSize int = 10, ----每頁顯示的記錄個數
- @page int = 1, ----要顯示那一頁的記錄
- @pageCount int = 1 output, ----]查詢結果分頁後的總頁數
- @Counts int = 1 output, ----查詢到的記錄數
- @fldSort nvarchar(100) = null, ----排序字段列表或條件
- @Sort bit = 0, ----排序方法,0爲升序,1爲降序
- @strCondition nvarchar(200) = null, ----查詢條件,不需where
- @ID nvarchar(50) ----主表的主鍵
- )
- AS
- SET NOCOUNT ON
- Declare @sqlTmp nvarchar(1000) ----存放動態生成的SQL語句
- Declare @strTmp nvarchar(1000) ----存放取得查詢結果總數的查詢語句
- Declare @strID nvarchar(1000) ----存放取得查詢開頭或結尾ID的查詢語句
- Declare @sqlSort nvarchar(200) ----存放臨時生成的排序條件
- Declare @intCounts int ----要移動的記錄數
- Declare @BeginID int ----開始的ID
- Declare @EndID int ----結束的ID
- --------首先生成排序方法---------
- if @Sort=0 --升序
- begin
- if not(@fldSort is null)
- set @sqlSort = ' Order by ' + @fldSort
- else
- set @sqlSort = ' Order by ' + @ID
- end
- else --降序
- begin
- if not(@fldSort is null)
- set @sqlSort = ' Order by ' + @fldSort + ' DESC'
- else
- set @sqlSort = ' Order by ' + @ID + ' DESC '
- end
- --------生成查詢語句--------
- --此處@strTmp爲取得查詢結果數量的語句
- if @strCondition is null --沒有設置顯示條件
- begin
- set @sqlTmp = @fldName + ' From ' + @tblName
- set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName
- set @strID = ' From ' + @tblName
- end
- else
- begin
- set @sqlTmp = + @fldName + ' From ' + @tblName
- set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName + ' where ' + @strCondition
- set @strID = ' From ' + @tblName + ' where ' + @strCondition
- end
- ----取得查詢結果總數量-----
- exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
- --取得分頁總數
- if @Counts <= @pageSize
- set @pageCount = 1
- else
- set @pageCount = (@Counts / @pageSize) + 1
- --計算要移動的記錄數
- if @page = 1
- set @intCounts = @pageSize
- else
- begin
- set @intCounts = (@page-1) * @pageSize + 1
- end
- -----取得分頁後此頁的第一條記錄的ID
- set @strID = 'select @BeginID=' + @ID + ' ' + @strID
- set @intCounts = @intCounts - @pageSize + 1
- set rowcount @intCounts
- exec sp_executesql @strID,N'@BeginID int out ',@BeginID out
- -----取得分頁後此頁的最後一條記錄的ID
- set @intCounts = @intCounts + @pageSize - 1
- print @intCounts
- set rowcount @intCounts
- exec sp_executesql @strID,N'@BeginID int out ',@EndID out
- ------恢復系統設置-----
- set rowcount 0
- SET NOCOUNT OFF
- ------返回查詢結果-----
- if @strCondition is null
- set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID + ' between ' + str(@BeginID) + ' and ' + str(@EndID)
- else
- set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID +' between ' + str(@BeginID) + ' and ' + str(@EndID) + ' and ' + @strCondition
- if not(@sqlSort is null)
- set @strTmp = @strTmp + @sqlSort
- exec sp_executesql @strTmp
dal層調用:異步
- public DataTable Select(string LDXX, string ZXBH, string JTBZ, string CLBZ, string KSSJ, string JSSJ, int CurrentPageIndex, string orderPX, out int count, out int totlepage)
- {
- StringBuilder strSql = new StringBuilder();
- StringBuilder strtj = new StringBuilder();
- StringBuilder st = new StringBuilder();
- if (!string.IsNullOrEmpty(ZXBH))
- {
- strtj.Append(" ZXBH = " + int.Parse(ZXBH) + " and ");
- }
- if (!string.IsNullOrEmpty(JTBZ))
- {
- strtj.Append(" JTBZ = " + JTBZ + " and ");
- }
- if (!string.IsNullOrEmpty(CLBZ))
- {
- strtj.Append(" CLBZ = " + CLBZ + " and ");
- }
- if (!string.IsNullOrEmpty(KSSJ))
- {
- strtj.Append(" LDSJ >='" + DateTime.Parse(KSSJ) + "' and ");
- }
- if (!string.IsNullOrEmpty(JSSJ))
- {
- strtj.Append(" LDSJ >='" + DateTime.Parse(JSSJ) + "' and ");
- }
- if (!string.IsNullOrEmpty(LDXX))
- {
- strtj.Append(" LDXX like '%" + LDXX + "%' and ");
- }
- strtj.Append(" 11=1 ");
- string strname = "procPageL";
- SqlParameter[] listp = {
- new SqlParameter("@TableName",SqlDbType.VarChar,200),
- new SqlParameter("@ReFieldsStr",SqlDbType.VarChar,200),
- new SqlParameter("@OrderString",SqlDbType.VarChar,200),
- new SqlParameter("@WhereString",SqlDbType.VarChar,200),
- new SqlParameter("@pageSize",SqlDbType.Int,4),
- new SqlParameter("@PageIndex",SqlDbType.Int,4),
- new SqlParameter("@TotalRecord",SqlDbType.Int,4) };
- int perpage = 10;
- listp[0].Value = "HW_HRXX";
- listp[1].Value = "JGBH,HJBH,LDXX,ZXBH,LDSJ,JTSJ,JTBZ,XYSC,CLBZ,ZXYY,CLSJ,THSC,JRFS,ZJSJ,GJSJ ";
- if (!string.IsNullOrEmpty(orderPX))
- {
- listp[2].Value = orderPX;
- }
- else
- {
- listp[2].Value = "HJBH DESC";
- }
- listp[3].Value = strtj.ToString();
- listp[4].Value = perpage;
- listp[5].Value = CurrentPageIndex;
- listp[6].Direction = ParameterDirection.Output;
- DataTable dt = DbHelperSQL.RunProcedure(strname, listp).Tables[0];
- totlepage = perpage;
- count = int.Parse(listp[6].Value.ToString());
- return dt;
- }
分頁方法ide
- /// <summary>
- /// 分頁代碼
- /// </summary>
- /// <param name="count">記錄個數</param>
- /// <param name="Curpage">當前頁</param>
- /// <param name="pageSize">每頁顯現的數量</param>
- /// <param name="url">url</param>
- /// <param name="pagetag">page</param>
- /// <returns></returns>
- public static string GetPageCode(int count, int Curpage, int pageSize, string url, string pagetag)
- {
- bool mark = false;
- if (pagetag == "")
- pagetag = "page";
- if (url.Contains("type"))
- {
- urlurl = url.Substring(0, url.LastIndexOf("type"));
- }
- if (url.Contains(pagetag))//判斷查詢字符串中是否包含page
- {
- mark = true;
- }
- if (url.IndexOf('?') > 0)//判斷查詢字符串中是否有其餘數據
- urlurl = url + "&";
- else
- urlurl = url + "?";
- if (mark)//若是查詢字符串中包含page則去除
- {
- urlurl = url.Substring(0, url.LastIndexOf(pagetag));
- }
- int countPage = (count % pageSize) == 0 ? count / pageSize : count / pageSize + 1;
- int priPage = ((Curpage - 1)) > 0 ? (Curpage - 1) : 1;
- int lastPage = ((Curpage + 1)) < countPage ? (Curpage + 1) : countPage;
- string link = "<a class='tabletrpage' href='#'";
- string code = link + " onClick=qq(1)>首頁</a> " + link + "onClick=qq(" + priPage.ToString() + ")>上頁</a> " + link + "onClick=qq(" + lastPage.ToString() + ")>下頁</a> " + link + "onClick=qq(" + countPage.ToString() + ")>末頁</a> 頁次:" + Curpage.ToString() + "/" + countPage + "頁 共" + count.ToString() + "條信息 ";
- code += "<select action='page' onchange=qq(this.value)>";
- for (int i = 1; i <= countPage; i++)
- {
- string selected = "";
- if (Curpage == i)
- selected = "selected='selected'";
- code += string.Format("<option value='{0}' {2}>{0}/{1}</option>", i, countPage, selected);
- }
- code += "</select>";
- return code;
- }
頁面後臺實現:優化
- DataTable dt = fwlog.Select(ldxx,zxbh,jtbz,clbz, kssj, jssj, page,orderPX,out count ,out totlepage);
- StringBuilder st = new StringBuilder();
- st.Append(" <table width='98%' align='center' border='1' align='center' cellpadding='3' cellspacing='0' style='border-collapse: collapse' class='TableBorderStyle' >");
- st.Append("<tr class='TableTRBgStyle'>");
- st.AppendFormat("<td align='center' >呼叫編號</td>");
- st.AppendFormat("<td align='center' >來電號碼</td>");
- st.AppendFormat("<td align='center' >來電時間</td>");
- st.AppendFormat("<td align='center' >座席編號</td>");
- st.AppendFormat("<td align='center' >座席名稱</td>");
- st.AppendFormat("<td align='center' >處理標誌</td>");
- st.AppendFormat("<td align='center' >註銷未接來電</td>");
- st.AppendFormat("<td align='center' >處理未接來電</td>");
- st.Append("</tr>");
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- st.AppendFormat("<tr>");
- st.AppendFormat("<td width='40%' align='center' >{0}</td>", dt.Rows[i]["HJBH"]);
- st.AppendFormat("<td width='40%' align='center' >{0}</td>", dt.Rows[i]["LDXX"]);
- st.AppendFormat("<td width='40%' align='center' >{0}</td>", dt.Rows[i]["LDSJ"]);
- st.AppendFormat("<td width='40%' align='center' >{0}</td>", dt.Rows[i]["ZXBH"]);
- if (dt.Rows[i]["ZXBH"].ToString() == "")
- {
- st.AppendFormat("<td width='10%' align='center' >{0}</td>", dt.Rows[i]["ZXBH"]);
- }
- else
- {
- st.AppendFormat("<td width='10%' align='center'>{0}</td>", tool.GetCZYMCbyID(int.Parse(dt.Rows[i]["ZXBH"].ToString())));
- }
- st.AppendFormat("<td width='40%' align='center' >{0}</td>", tool.GetDMName("CLBZ", dt.Rows[i]["CLBZ"].ToString()));
- if (dt.Rows[i]["CLBZ"].ToString() == "0" || dt.Rows[i]["CLBZ"].ToString() == "2")
- {
- st.AppendFormat("<td width='30%' align='center' ><img src='../../../p_w_picpaths/txt.gif' onclick='dd()' id='{0}'></td>", dt.Rows[i]["HJBH"]);
- }
- else
- {
- st.AppendFormat("<td width='30%' align='center' ><img src='../../../p_w_picpaths/txt.gif' onclick='tt({0})' id='{0}'></td>", dt.Rows[i]["HJBH"]);
- }
- //st.AppendFormat("<td width='30%' align='center' ><img src='F:\\haiyan\\kfzx\\p_w_picpaths\\cc.ico' onclick='tt({0},{1})' id='{0}'></td>",dt.Rows[i]["HJBH"],dt.Rows[i]["CLBZ"]);
- st.AppendFormat("<td width='30%' align='center' ><img src='../../../p_w_picpaths/clbtn.gif'></td>");
- st.Append("</tr>");
- }
- st.Append("<tr class='TableBottom'>");
- st.Append(" <td colspan='8'><center>" + tool.GetPageCode(fwlog.Count(ldxx ,zxbh ,jtbz,clbz, kssj, jssj), page, 10, "", "") + "</center></td>");
- st.Append("</tr>");
- st.Append("</table>");
- pagestr = st.ToString();
頁面前臺異步刷新:ui
- //=========分頁=====================
- function qq(dat)
- {
- q=dat;
- $.get("ym_zxxt_ywsl_ldjl_selcet.aspx?z="+z,{cur:dat,LDXX:$("#LDXX").val()},function(data){
- $("#select").html(data);
- })
- z++;
- }
比較簡單的代碼 沒作什麼過多的講解 比較容易看懂this