在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 ;
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器