導讀:在上篇文章中,介紹了用假分頁實現數據的分頁顯示 ,而避免了去拖動滾動條。但,假分頁在分頁的同時,其實是拖垮了查詢效率的。每一次分頁都得重新查詢一遍數據,那么有沒有方法可以同時兼顧效率和分頁呢,那就是真分頁。
一、真分頁概述
相對於假分頁,真分頁是什么呢?
真分頁就是每次換頁的時候,只查詢相對應的頁碼的那幾條數據。比如說,每頁顯示5條數據,那么窗體加載的時候,就只是查詢了符合條件的前5條數據。如果點擊第10頁,那么查詢的就是第46條數據至第50條數據。這樣,每次打開一頁的速度是一樣的,而且效率會很高。因為每次查詢的數據是很小、有限的。
二、實現真分頁
2.1,添加控件
在實現假分頁的過程中,需要用到第三方控件AspNetPager。需要先下載這個控件的DLL文件,然后拖動至工具箱中,然后再在界面中使用它。
2.2,界面設計
備注:分頁控件的顯示風格和形式,可以由用戶自己確定。
2.3,代碼實現
<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:24px;">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; namespace WebApplication1 { public partial class truePage : System.Web.UI.Page { /// <summary> /// 窗體加載時,沒有改變頁數的數據顯示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack ) { //窗體打卡時,起始數據編號為0,終止數據編號為每頁的信息條數 int intStartIndex = ANP.PageSize * 0; int intEndIndex = ANP.PageSize * 1; <span style="color:#ff0000;"><strong> ANP.RecordCount = MyAllData().Rows.Count;</strong></span> //綁定數據 GridView1.DataSource = MyPartData(intStartIndex, intEndIndex); GridView1.DataBind(); } } /// <summary> /// 改變了頁數的數據顯示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ANP_PageChanged(object sender, EventArgs e) { //起始數據編號=每頁的信息容量*(當前頁數-1)+1; //例:第六頁,每頁顯示5條,第六頁的起始數據=5*5+1=26; int intStartIndex=ANP.PageSize * (ANP.CurrentPageIndex-1)+1; int intEndIndex = ANP.PageSize * ANP.CurrentPageIndex; GridView1.DataSource = MyPartData(intStartIndex,intEndIndex ); GridView1.DataBind(); } /// <summary> /// 分頁查詢 /// </summary> /// <param name="intStartIndex">開始的數據編號</param> /// <param name="intEndIndex">結束的數據編號</param> /// <returns>表格</returns> private static DataTable MyPartData(int intStartIndex,int intEndIndex) { SqlConnection con = new SqlConnection("server=.;database=newssystem;uid=sa;password=123456"); con.Open(); string strCmd = "Proc_TruePage";//存儲過程 SqlParameter[] paras = new SqlParameter[] { new SqlParameter ("@intStartIndex",intStartIndex), new SqlParameter ("@intEndIndex",intEndIndex) }; SqlCommand cmd = new SqlCommand(strCmd ,con ); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(paras); SqlDataReader sdr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(sdr); con.Close(); return dt; } /// <summary> /// 全部查詢 /// </summary> /// <returns></returns> private static DataTable MyAllData() { SqlConnection con = new SqlConnection("server=.;database=newssystem;uid=sa;password=123456"); con.Open(); string strCmd = "select * from comment"; SqlCommand cmd = new SqlCommand(strCmd, con); SqlDataReader sdr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(sdr); con.Close(); return dt; } } }</span></span>
2.4,存儲過程
<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:24px;">USE [newssystem] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <HHX> -- Create date: <2015/5/9> -- Description: <通過行號的確立,實現分頁查詢數據> -- ============================================= ALTER PROCEDURE [dbo].[Proc_TruePage ] @intStartIndex int,--開始的頁 @intEndIndex int--結束的頁 as BEGIN SET NOCOUNT ON; --為獲取到行號的表建立一個臨時表 with TempTable as ( select ROW_NUMBER () over(order by id desc) as RowsIndex,* from comment ) --查詢從開始頁到結束頁的數據 select * from TempTable where RowsIndex between @intStartIndex and @intEndIndex ; END </span></span>
說明:在實現真分頁的時候,也查詢了一遍整個數據,目的是為了獲取總共有多少條數據。不過,它只通篇查詢了一遍數據庫,相對於假分頁來說,也是夠效率的。但考慮到數據量大的話,真的也能讓人等一會兒了。所以,可以通過減少查詢字段,或者說依靠行號來解決。
2.5,實現效果
三、個人感受
只要不滿足於現象,也就是不將就,就能找到更好的。學習了也有一段時間了,也該關心關心性能了,只是實現了功能還是不夠的。