<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      使用ASP.NET完成分頁管理器

      [摘要]在DataGrid的web版控件中提供了自動分頁的功能,但是我從來沒用過它,因為它實現的分頁只是一種假相。我們為什么需要分頁?那是因為符合條件的記錄可能很多,如果一次讀取所有的記錄,不僅延長獲取數據的時間,而且也極度浪費內存。而分頁的存在的主要目的正是為了解決這兩個問題(當然,也不排除為了UI美觀...
      在DataGrid的web版控件中提供了自動分頁的功能,但是我從來沒用過它,因為它實現的分頁只是一種假相。我們為什么需要分頁?那是因為符合條件的記錄可能很多,如果一次讀取所有的記錄,不僅延長獲取數據的時間,而且也極度浪費內存。而分頁的存在的主要目的正是為了解決這兩個問題(當然,也不排除為了UI美觀的需要而使用分頁的)。而web版的DataGrid是怎樣實現分頁的了?它并沒有打算解決上述兩個問題,而還是一次讀取所有的數據,然后以分頁的樣子表現出來。這是對效率和內存的極大損害! 

        于是我自己實現了分頁管理器IPaginationManager ,IPaginationManager 每次從數據庫中讀取指定的任意一頁,并且可以緩存指定數量的page。這個分頁管理器的主要特點是:

        (1)支持隨機跳轉。這是通過嵌套Select語句實現的。

        (2)支持緩存。通過EnterpriseServerBase.DataStructure.FixCacher進行支持。

        先來看看IPaginationManager接口的定義:

      public interface IPaginationManager
      {
       void Initialize(DataPaginationParas paras) ;
       void Initialize(IDBAccesser accesser ,int page_Size ,string whereStr ,string[] fields) ;//如果選擇所有列, fields可傳null

       DataTable GetPage(int index) ; //取出第index頁
       DataTable CurrentPage() ;
       DataTable PrePage() ;
       DataTable NextPage() ;

       int PageCount{get ;}
       int CacherSize{get; set; }
      }
        這個接口定義中,最主要的是GetPage()方法,實現了這個方法,其它的三個獲取頁面的方法CurrentPage、PrePage、NextPage也就非常容易了。另外,CacherSize屬性可以讓我們指定緩存頁面的數量。如果不需要緩存,則設置其值<=0,如果需要無限緩存,則值為Int.MaxValue。

        IPaginationManager接口中的第二個Initialize方法,你不要關心,它是給XCodeFactory生成的數據層使用了,我們來看看第一個Initialize方法的參數類型DataPaginationParas的定義:

      public class DataPaginationParas
      {
       public int PageSize = 10 ; 
       public string[] Fields = {"*"}; //要搜索出的列,"*"表示所有列

       public string ConnectString ;
       public string TableName ; 
       public string WhereStr ; //搜索條件的where字句

       public DataPaginationParas(string connStr ,string tableName ,string whereStr)
       {
        this.ConnectString = connStr ;
        this.TableName = tableName ;
        this.WhereStr = whereStr ;
       } 

       #region GetFiedString
       public string GetFiedString()
       {
        if(this.Fields == null) 
        {
         this.Fields = newstring[] {"*"} ;
        }

        string fieldStrs = "" ;

        for(int i=0 ;i  {
         fieldStrs += " " + this.Fields[i] ;
         if(i != (this.Fields.Length -1))
         {
          fieldStrs += " , " ;
         }
         else
         {
          fieldStrs += " " ;
         }
        }

        return fieldStrs ;
       }
       #endregion

      }

        DataPaginationParas.GetFiedString用于把要搜索的列形成字符串以便嵌入到SQL語句中。DataPaginationParas中的其它字段的意思都很明顯。

        現在來看看分頁管理器的實現了:

      public class PaginationManager :IPaginationManager
      {
       private DataPaginationParas theParas ;
       private IADOBase adoBase ; 
       private DataTable curPage = null ;
       private int itemCount = 0 ;
       private int pageCount = -1 ; 
       private int curPageIndex = -1 ;

       private FixCacher fixCacher = null ;
       private string fieldStrs = "" ;

       /// 
       /// cacheSize 小于等于0 -- 表示不緩存 ,Int.MaxValue -- 緩存所有
       /// 
       public PaginationManager(int cacheSize)
       {
        if(cacheSize == int.MaxValue)
        {
         this.fixCacher = new FixCacher() ;
        }
        else if(cacheSize >0)
        {
         this.fixCacher = new FixCacher(cacheSize) ;
        }
        else
        {
         this.fixCacher = null ;
        }
       } 

       public PaginationManager()
       {}

       #region IDataPaginationManager 成員
       public int CacherSize
       {
        get
        {
         if(this.fixCacher == null)
         {
          return 0 ;
         }
         return this.fixCacher.Size ;
        }
        set
        {
         if(this.fixCacher == null)
         {
          this.fixCacher = new FixCacher(value) ;
         }
         else
         {
          this.fixCacher.Size = value ;
         }

      [1] [2]  下一頁




      日韩精品一区二区三区高清