簡單的預存程序實現asp.net中的分頁

來源:互聯網
上載者:User

我們在許多程式中都要用到查詢資料,當資訊量很大的時候,我們在檢索資訊,頁面顯示時候需要花費大量的

時間導致效率低下!
我們在檢索資訊的時候,只把當前需要的資料檢索出來,也就是用常說的分頁處理來提高處理效率!
瀏覽網頁,發現很多網站,論壇都是採用asp.net中的GridView內建的分頁功能來處理資料分頁。看起來非常簡

單。實質上,當我們啟動GridView中的分頁後,資料來源關聯資料來源控制項,依舊會載入所有的資訊。因此使用

GridView中的分頁功能實質上在資料量較大的情況下是效率十分低下的!
我在這談談用預存程序來實現分頁功能。
在這我給出我使用的表
create table New(
Num     identity(1,1) not null,
Sort    varchar(64)   not null, 
Title   varchar(128)  not null,
Author  varchar(64)   not null,
Matter  text          not null
)
go

構造預存程序如下:
CREATE PROCEDURE pro_New
    @PageIndex INT,  //顯示的當前頁數
    @PageSize  INT   //每頁顯示大小

AS

/* 基於SQL SERVER 2005 */

SELECT Num,Title,SerialNumber FROM
(SELECT Title,Num,ROW_NUMBER() OVER (ORDER BY Num Asc) AS SerialNumber FROM New ) AS T
WHERE T.SerialNumber > ((@PageIndex-1) * @PageSize)  and T.SerialNumber <= (@PageIndex*@PageSize)

go

其中定義的SerialNumber是為排序後的編號,因為把Num設定為自增長時,在刪除記錄後有可能出現空缺位!
特別注意的是,由於資料庫中的第一頁實際上是第0頁。

 public partial class _Default : System.Web.UI.Page
    {
        private NewsBiz biz = new NewsBiz();//此處調用商務邏輯層方法

        public int pageSize = 10;//此為首頁顯示的資料為10條記錄
        private static readonly string connectionStrings =        

ConfigurationManager.ConnectionStrings["NewsData"].ConnectionString;//串連Web.config

        protected void Page_Load(object sender, EventArgs e)
        {
           

                 int pageCount = Convert.ToInt32(cmd.ExecuteScalar());//總記錄

                int page = Convert.ToInt32(Request.QueryString.Get("id"));//頁面傳值擷取頁碼

                if (page == 0)
                {
                    page = 1;
                }

                float temp = (float)pageCount / pageSize;//頁數

                if (page >= Convert.ToInt32(Math.Ceiling(temp)))
                {
                    page = Convert.ToInt32(Math.Ceiling(temp));
                }

                //在此可綁定讀取的資料來源,也可自訂嵌入HTML代碼
                IList<NewsInfo> news = biz.SearchExec(page, pageSize);
                GridView1.DataSource = news;
                GridView1.DataBind();
                //string html = null;
                //for (int i = 0; i < news.Count; i++)
                //{

                //    html += "<div align=\"center\">";
                //    html += "<a href=\"view.aspx?id=" + news[i].Num + "\" target=\"_blank>\"";
                //    html += "</a>";
                //    html += news[i].Title;
                //    html += "</div>";

 

                //}
                //showTd.InnerHtml = html;

                if (!IsPostBack)//個人覺得這裡很重要,因為對於asp.net的page(不是上面說的頁碼,是

page類)存在一個生命週期,每次向伺服器發送請求,page都會被銷毀重新load
                {
                    pageNowTextBox.Text = page.ToString();
                }

        }

還有上一頁,下一頁等其他代碼就不羅列出來了!明白了分頁的思想實現起來都比較方便。
也許我這中方法不是很好的方法!但是與GridView的分頁相比,其效率應該更高!有時間再探討其他分頁方法

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.