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 ;
}
}
}
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 ;
}
}
///
/// getpage 取出指定的一頁
///
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就可以了。新聞熱點
疑難解答
圖片精選