国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

利用ASP.NET實現分頁管理器

2024-07-10 12:54:58
字體:
來源:轉載
供稿:網友
    在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 ;
   }
  }
 }
 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就可以了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洪湖市| 博白县| 宣恩县| 承德县| 札达县| 正蓝旗| 固安县| 蒙城县| 井研县| 南江县| 六枝特区| 改则县| 哈巴河县| 贺兰县| 桃江县| 兖州市| 杭州市| 和田县| 鲁甸县| 西盟| 项城市| 靖州| 东海县| 绥宁县| 高雄县| 仁怀市| 丹巴县| 廉江市| 邵东县| 昌宁县| 和龙市| 沙坪坝区| 繁昌县| 汽车| 江山市| 金阳县| 汕尾市| 上思县| 合川市| 清徐县| 信宜市|