海量(大量)資料GridView顯示高效率分頁實現方法

來源:互聯網
上載者:User
海量(大量)資料GridView顯示高效率分頁實現方法2009-12-05 11:49

來源網路

問題:

      GridView顯示海量資料時效率極其低,每次要從資料庫從取出海量資料,這明顯不現實。

解決方案:

       顯示多少,就從資料庫中讀取多少資料,即每次取GridView顯示的一個頁面資料,點擊下一頁是,再取下一頁相應的資料。

真的是很簡單,但如果對asp.net不熟悉的人來說,還是有點難度。

就比如說:當頁面重新整理時,怎樣使一個變數的值保持不變呢?很暈是吧

下面我們說分析實現方法:

1、重新整理頁面,保持變數值不變

先講怎樣在重新整理頁面是使一個變數值保持不變,以前我都採用頁面傳值法,總覺得有點怪,本來就是本頁的資料還要通過頁面傳值來儲存。

後來在網上發現VewState這東東,有點像Session,用法基本上一樣。

他們的區別是:Session是在頁面之間,而ViewState是在它自己的頁面保持一個值。

用法:

        ViewState["PageCounts"] = 3;

         下次要用時直接取出來就OK了。

 

2、GridView海量資料高效分頁實現代碼

 

下面代碼來自互連網

 

   const int PageSize=20;//定義每頁顯示記錄
   int PageCount,RecCount,CurrentPage,Pages,JumpPage;//定義幾個儲存分頁參數變數

   private void Page_Load(object sender, System.EventArgs e)
   {
    if(!IsPostBack)
    {
     RecCount = Calc();//通過Calc()函數擷取總記錄數
     PageCount = RecCount/PageSize + OverPage();

   //計算總頁數(加上OverPage()函數防止有餘數造成顯示資料不完整)

     ViewState["PageCounts"] = RecCount/PageSize - ModPage();

      //儲存總頁參數到ViewState(減去ModPage()函數防止SQL語句執行時溢出查詢範圍,可以用預存程序分頁演算法來理解這句)
     ViewState["PageIndex"] = 0;//儲存一個為0的頁面索引值到ViewState
     ViewState["JumpPages"] = PageCount;

        //儲存PageCount到ViewState,跳頁時判斷使用者輸入數是否超出頁碼範圍
     //顯示LPageCount、LRecordCount的狀態
     LPageCount.Text = PageCount.ToString();
     LRecordCount.Text = RecCount.ToString();
     //判斷跳頁文字框失效
     if(RecCount <= 20)
      gotoPage.Enabled = false;
     TDataBind();//調用資料繫結函數TDataBind()進行資料繫結運算
    }
   }
   //計算餘頁
   public int OverPage()
   {
    int pages = 0;
    if(RecCount%PageSize != 0)
     pages = 1;
    else
     pages = 0;
    return pages;
   }
      //計算餘頁,防止SQL語句執行時溢出查詢範圍
   public int ModPage()
   {
    int pages = 0;
    if(RecCount%PageSize == 0 && RecCount != 0)
     pages = 1;
    else
     pages = 0;
    return pages;
   }
        /*
   *計算總記錄的靜態函數
   *本人在這裡使用靜態函數的理由是:如果引用的是待用資料或靜態函數,連接器會最佳化產生代碼,去掉動態重定位項(對

海量資料表分頁效果更明顯)。
   *希望大家給予意見、如有不正確的地方望指正。
   */
   public static int Calc()
   {
    int RecordCount = 0;
    SqlCommand MyCmd = new SqlCommand("select count(*) as co from redheadedfile",MyCon());
    SqlDataReader dr = MyCmd.ExecuteReader();
    if(dr.Read())
     RecordCount = Int32.Parse(dr["co"].ToString());
    MyCmd.Connection.Close();
    return RecordCount;
   }
        //資料庫連接語句(從Web.Config中擷取)
   public static SqlConnection MyCon()
   {
    SqlConnection MyConnection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
    MyConnection.Open();
    return MyConnection;
   }
//對四個按鈕(首頁、上一頁、下一頁、尾頁)返回的CommandName值進行操作
   private void Page_OnClick(object sender, CommandEventArgs e)
   {
    CurrentPage = (int)ViewState["PageIndex"];

   //從ViewState中讀取頁碼值儲存到CurrentPage變數中進行參數運算
    Pages = (int)ViewState["PageCounts"];//從ViewState中讀取總頁參數運算

    string cmd = e.CommandName;
    switch(cmd)//篩選CommandName
    {
     case "next":
      CurrentPage++;
      break;
     case "prev":
      CurrentPage--;
      break;
     case "last":
      CurrentPage = Pages;
      break;
     default:
      CurrentPage = 0;
      break;
    }
    ViewState["PageIndex"] = CurrentPage;

//將運算後的CurrentPage變數再次儲存至ViewState

    TDataBind();//調用資料繫結函數TDataBind()
   }

   private void TDataBind()
   {
    CurrentPage = (int)ViewState["PageIndex"];

   //從ViewState中讀取頁碼值儲存到CurrentPage變數中進行按鈕失效運算
    Pages = (int)ViewState["PageCounts"];

    //從ViewState中讀取總頁參數進行按鈕失效運算
    //判斷四個按鈕(首頁、上一頁、下一頁、尾頁)狀態
    if (CurrentPage + 1 > 1)
    {
     Fistpage.Enabled = true;
     Prevpage.Enabled = true;
    }
    else
    {
     Fistpage.Enabled = false;
     Prevpage.Enabled = false;
    }
    if (CurrentPage == Pages)
    {
     Nextpage.Enabled = false;
     Lastpage.Enabled = false;
    }
    else
    {
     Nextpage.Enabled = true;
     Lastpage.Enabled = true;
    }
            //資料繫結到DataList控制項
    DataSet ds = new DataSet();
    //核心SQL語句,進行查詢運算(決定了分頁的效率:)
    SqlDataAdapter MyAdapter = new SqlDataAdapter("Select Top "+PageSize+" * from redheadedfile where id

not in(select top "+PageSize*CurrentPage+" id from redheadedfile order by id asc) order by id asc",MyCon());
    MyAdapter.Fill(ds,"news");
    datalist1.DataSource = ds.Tables["news"].DefaultView;
    datalist1.DataBind();
   //顯示Label控制項LCurrentPaget和文字框控制項gotoPage狀態
    LCurrentPage.Text = (CurrentPage+1).ToString();
    gotoPage.Text = (CurrentPage+1).ToString();
    //釋放SqlDataAdapter
    MyAdapter.Dispose();
   }

      //跳頁代碼
   private void gotoPage_TextChanged(object sender, System.EventArgs e)
   {
    try
    {
     JumpPage = (int)ViewState["JumpPages"];

//從ViewState中讀取可用頁數值儲存到JumpPage變數中

     //判斷使用者輸入值是否超過可用頁數範圍值
     if(Int32.Parse(gotoPage.Text) > JumpPage || Int32.Parse(gotoPage.Text) <= 0)
     

Response.Write("<script>alert('頁碼範圍越界!');location.href='WebForm8.aspx'</script>");
     else
     {
      int InputPage = Int32.Parse(gotoPage.Text.ToString()) - 1;

//轉換使用者輸入值儲存在int型InputPage變數中
      ViewState["PageIndex"] = InputPage;

//寫入InputPage值到ViewState["PageIndex"]中
      TDataBind();

//調用資料繫結函數TDataBind()再次進行資料繫結運算
     }
    }
      //捕獲由使用者輸入不正確資料類型時造成的異常
    catch(Exception exp)
    {
     Response.Write("<script>alert('"+exp.Message+"');location.href='WebForm8.aspx'</script>");
    }
   }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.