分页管理器实现

简介: 在DataGrid的web版控件中提供了自动分页的功能,但是我从来没用过它,因为它实现的分页只是一种假相。我们为什么需要分页?那是因为符合条件的记录可能很多,如果一次读取所有的记录,不仅延长获取数据的时间,而且也极度浪费内存。

    在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  =   new   string [] { " * " } ;
            }

            
string  fieldStrs  =   ""  ;

            
for ( int  i = 0  ;i < this .Fields.Length ;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     =   ""  ;

        
///   <summary>
        
///  cacheSize 小于等于0 -- 表示不缓存 ,Int.MaxValue -- 缓存所有
        
///   </summary>         
         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 ;
                }
            }
        }
        
public   int  PageCount
        {
            
get
            {
                
if ( this .pageCount  ==   - 1 )
                {
                    
string  selCountStr  =   string .Format( " Select count(*) from {0} {1} "  , this .theParas.TableName , this .theParas.WhereStr) ;
                    DataSet ds 
=   this .adoBase.DoQuery(selCountStr) ;
                    
this .itemCount  =   int .Parse(ds.Tables[ 0 ].Rows[ 0 ][ 0 ].ToString()) ;
                    
this .pageCount  =   this .itemCount / this .theParas.PageSize ;
                    
if (( this .itemCount % this .theParas.PageSize  >   0 ))
                    {
                        
++   this .pageCount ;
                    }
                }

                
return   this .pageCount ;
            }
        }

        
///   <summary>
        
///  GetPage 取出指定的一页
        
///   </summary>         
         public  DataTable GetPage( int  index)
        {
            
if (index  ==   this .curPageIndex)
            {
                
return   this .curPage ;
            }

            
if ((index  <   0 ||  (index  >  ( this .PageCount - 1 )))
            {
                
return   null ;
            }

            DataTable dt 
=   this .GetCachedObject(index) ;

            
if (dt  ==   null )
            {
                
string  selectStr  =   this .ConstrutSelectStr(index) ;
                DataSet ds 
=   this .adoBase.DoQuery(selectStr) ;
                dt 
=  ds.Tables[ 0 ] ;

                
this .CacheObject(index ,dt) ;
            }

            
this .curPage       =  dt ;
            
this .curPageIndex  =  index ;
            
return   this .curPage ;
        }

        
private  DataTable GetCachedObject( int  index)
        {
            
if ( this .fixCacher  ==   null )
            {
                
return   null  ;
            }

            
return  (DataTable) this .fixCacher[index] ;
        }

        
private   void  CacheObject( int  index ,DataTable page)
        {
            
if ( this .fixCacher  !=   null )
            {
                
this .fixCacher.PutIn(index ,page) ;
            }
        }

        
public  DataTable CurrentPage()
        {
            
return   this .curPage ;
        }

        
public  DataTable PrePage()
        {
            
return   this .GetPage(( -- this .curPageIndex)) ;
        }

        
public  DataTable NextPage()
        {
            
return   this .GetPage(( ++ this .curPageIndex)) ;
        }    
    
        
private   string  ConstrutSelectStr( int  pageIndex)
        {
            
if (pageIndex  ==   0 )
            {
                
return   string .Format( " Select top {0} {1} from {2} {3} ORDER BY ID "  , this .theParas.PageSize , this .fieldStrs , this .theParas.TableName , this .theParas.WhereStr) ;
            }

            
int  innerCount      =   this .itemCount  -   this .theParas.PageSize * pageIndex ;
            
string  innerSelStr  =   string .Format( " Select top {0} {1} from {2} {3} ORDER BY ID DESC  "  ,innerCount ,  this .fieldStrs , this .theParas.TableName , this .theParas.WhereStr) ;
            
string  outerSelStr  =   string .Format( " Select top {0} * from ({1}) DERIVEDTBL ORDER BY ID "  , this .theParas.PageSize ,innerSelStr) ;

            
return  outerSelStr ;
        }

        
#region  Initialize
        
public   void  Initialize(IDBAccesser accesser,  int  page_Size,  string  whereStr,  string [] fields)
        {
            
this .theParas  =   new  DataPaginationParas(accesser.ConnectString ,accesser.DbTableName ,whereStr) ;
            
this .theParas.Fields  =  fields ;
            
this .theParas.PageSize  =  page_Size ;
        
            
this .fieldStrs  =   this .theParas.GetFiedString() ;    
            
this .adoBase  =   new  SqlADOBase( this .theParas.ConnectString) ;
        }    
        
        
public   void  Initialize(DataPaginationParas paras)
        {
            
this .theParas  =  paras ;
            
this .fieldStrs  =   this .theParas.GetFiedString() ;    
            
this .adoBase  =   new  SqlADOBase( this .theParas.ConnectString) ;
        }

        
#endregion

        
#endregion
    }

    了解这个类的实现,可以从GetPage(int index)方法入手,另外私有方法ConstrutSelectStr()的实现说明了如何使用嵌套sql语句进行随机分页搜索。
    最后,关于分页管理器,需要指出的是,搜索对应的表必须有一个名为"ID"的主键--这是唯一的要求。另外,分页管理器实现用到的数据访问低阶封装IADOBase定义于EnterpriseServerBase类库中。
    使用分页管理器是很简单的,加上UI界面后,只要把返回的DataTable绑定到DataGrid就可以了:)



 

目录
相关文章
|
4月前
|
前端开发
bladex自定义分页
bladex自定义分页
|
11月前
|
SQL 前端开发 Java
通用分页-后台
通用分页-后台
85 0
|
3月前
|
Java 数据库连接 mybatis
MyBatisPlus分页功能制作未完
MyBatisPlus分页功能制作未完
|
4月前
|
存储 算法 API
LabVIEW创建自定义书签管理器
LabVIEW创建自定义书签管理器
23 0
|
4月前
|
存储 关系型数据库 C语言
开启内存分页
开启内存分页
50 0
|
数据库 C# 数据库管理
C#:Winfrom 实现DataGridView 自定义分页
今天给大家分享Winform实现DataGridView 自定义分页的案例,感兴趣的朋友可以一起来学习一下。 采用技术:C\#+Winform+Dapper+SQLite。
C#:Winfrom 实现DataGridView 自定义分页
|
数据库
分页控件的使用能不能再简单一点呢,能不能一个页面搞定所有的列表需求?
目的: 1、一个页面(DataList.aspx)可以显示多个模块的列表功能。      一般是有一个列表需求就需要一个aspx文件,如果有100个列表,那么就会有100个aspx文件,这么多的文件(包括.aspx.cs文件)里面的内容基本是一样的,这样写起来麻烦,管理起来也不容易,命名就是一个比较头痛的问题。
1066 0