RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
ASP.NET分页管理器的设计及实现

在DataGrid的web版控件中提供了自动分页的功能,但是我从来没用过它,因为它实现的分页只是一种假相。我们为什么需要分页?那是因为符合条件的记录可能很多,如果一次读取所有的记录,不仅延长获取数据的时间,而且也极度浪费内存。而分页的存在的主要目的正是为了解决这两个问题(当然,也不排除为了UI美观的需要而使用分页的)。而web版的DataGrid是怎样实现分页的了?它并没有打算解决上述两个问题,而还是一次读取所有的数据,然后以分页的样子表现出来。这是对效率和内存的极大损害!

成都创新互联公司是专业的平阳网站建设公司,平阳接单;提供成都网站建设、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行平阳网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

于是我自己实现了ASP.NET分页管理器IPaginationManager ,IPaginationManager 每次从数据库中读取指定的任意一页,并且可以缓存指定数量的page。这个分页管理器的主要特点是:

(1)支持随机跳转。这是通过嵌套Select语句实现的。

(2)支持缓存。通过EnterpriseServerBase.DataStructure.FixCacher进行支持。

先来看看IPaginationManager接口的定义:

 
 
 
  1. public interface IPaginationManager
  2. {
  3. void Initialize(DataPaginationParas paras) ;
  4. void Initialize(IDBAccesser accesser ,
  5. int page_Size ,string whereStr ,string[] 
  6. fields) ;//如果选择所有列,fields可传null
  7. DataTable GetPage(int index) ; //取出第index页
  8. DataTable CurrentPage() ;
  9. DataTable PrePage() ;
  10. DataTable NextPage() ;
  11. int PageCount{get ;}
  12. int CacherSize{get; set; }
  13. }

这个接口定义中,最主要的是GetPage()方法,实现了这个方法,其它的三个获取页面的方法CurrentPage、PrePage、NextPage也就非常容易了。另外,CacherSize属性可以让我们指定缓存页面的数量。如果不需要缓存,则设置其值<=0,如果需要无限缓存,则值为Int.MaxValue。

IPaginationManager接口中的第二个Initialize方法,你不要关心,它是给XCodeFactory生成的数据层使用了,我们来看看第一个Initialize方法的参数类型DataPaginationParas的定义:

 
 
 
  1. public class DataPaginationParas
  2. {
  3. public int PageSize = 10 ; 
  4. public string[] Fields = {"*"}; 
  5. //要搜索出的列,"*"表示所有列
  6. public string ConnectString ;
  7. public string TableName ; 
  8. public string WhereStr ; 
  9. //搜索条件的where字句
  10. public DataPaginationParas
  11. (string connStr ,string tableName ,
  12. string whereStr)
  13. {
  14. this.ConnectString = connStr ;
  15. this.TableName = tableName ;
  16. this.WhereStr = whereStr ;
  17. } 
  18. #region GetFiedString
  19. public string GetFiedString()
  20. {
  21. if(this.Fields == null) 
  22. {
  23. this.Fields = new string[] {"*"} ;
  24. }
  25. string fieldStrs = "" ;
  26. for(int i=0 ;i
  27. {
  28. fieldStrs += " " + this.Fields[i] ;
  29. if(i != (this.Fields.Length -1))
  30. {
  31. fieldStrs += " , " ;
  32. }
  33. else
  34. {
  35. fieldStrs += " " ;
  36. }
  37. }
  38. return fieldStrs ;
  39. }
  40. #endregion
  41. }

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

现在来看看ASP.NET分页管理器的实现了:

 
 
 
  1. public class PaginationManager :IPaginationManager
  2. {
  3. private DataPaginationParas theParas ;
  4. private IADOBase adoBase ; 
  5. private DataTable curPage = null ;
  6. private int itemCount = 0 ;
  7. private int pageCount = -1 ; 
  8. private int curPageIndex = -1 ;
  9. private FixCacher fixCacher = null ;
  10. private string fieldStrs = "" ;
  11. /// 
  12. /// cacheSize 小于等于0 -- 表示不缓存 ,
  13. Int.MaxValue -- 缓存所有
  14. ///
  15.    
  16. public PaginationManager(int cacheSize)
  17. {
  18. if(cacheSize == int.MaxValue)
  19. {
  20. this.fixCacher = new FixCacher() ;
  21. }
  22. else if(cacheSize >0)
  23. {
  24. this.fixCacher = new FixCacher(cacheSize) ;
  25. }
  26. else
  27. {
  28. this.fixCacher = null ;
  29. }
  30. } 
  31. public PaginationManager()
  32. {}
  33. #region IDataPaginationManager 成员
  34. public int CacherSize
  35. {
  36. get
  37. {
  38. if(this.fixCacher == null)
  39. {
  40. return 0 ;
  41. }
  42. return this.fixCacher.Size ;
  43. }
  44. set
  45. {
  46. if(this.fixCacher == null)
  47. {
  48. this.fixCacher = new FixCacher(value) ;
  49. }
  50. else
  51. {
  52. this.fixCacher.Size = value ;
  53. }
  54. }
  55. }
  56. public int PageCount
  57. {
  58. get
  59. {
  60. if(this.pageCount == -1)
  61. {
  62. string selCountStr = string.Format
  63. ("Select count(*) from {0} {1}" ,this.theParas.
  64. TableName ,this.theParas.WhereStr) ;
  65. DataSet ds = this.adoBase.DoQuery(selCountStr) ;
  66. this.itemCount = int.Parse(ds.Tables[0].
  67. Rows[0][0].ToString()) ;
  68. this.pageCount = this.itemCount/this.
  69. theParas.PageSize ;
  70. if((this.itemCount%this.theParas.PageSize > 0))
  71. {
  72. ++ this.pageCount ;
  73. }
  74. }
  75. return this.pageCount ;
  76. }
  77. }
  78. /// 
  79. /// GetPage 取出指定的一页
  80. ///
  81.    
  82. public DataTable GetPage(int index)
  83. {
  84. if(index == this.curPageIndex)
  85. {
  86. return this.curPage ;
  87. }
  88. if((index < 0) || (index > (this.PageCount-1)))
  89. {
  90. return null;
  91. }
  92. DataTable dt = this.GetCachedObject(index) ;
  93. if(dt == null)
  94. {
  95. string selectStr = this.ConstrutSelectStr(index) ;
  96. DataSet ds = this.adoBase.DoQuery(selectStr) ;
  97. dt = ds.Tables[0] ;
  98. this.CacheObject(index ,dt) ;
  99. }
  100. this.curPage = dt ;
  101. this.curPageIndex = index ;
  102. return this.curPage ;
  103. }
  104. private DataTable GetCachedObject(int index)
  105. {
  106. if(this.fixCacher == null)
  107. {
  108. return null ;
  109. }
  110. return (DataTable)this.fixCacher[index] ;
  111. }
  112. private void CacheObject(int index ,DataTable page)
  113. {
  114. if(this.fixCacher != null)
  115. {
  116. this.fixCacher.PutIn(index ,page) ;
  117. }
  118. }
  119. public DataTable CurrentPage()
  120. {
  121. return this.curPage ;
  122. }
  123. public DataTable PrePage()
  124. {
  125. return this.GetPage((--this.curPageIndex)) ;
  126. }
  127. public DataTable NextPage()
  128. {
  129. return this.GetPage((++this.curPageIndex)) ;
  130. } 
  131. private string ConstrutSelectStr(int pageIndex)
  132. {
  133. if(pageIndex == 0)
  134. {
  135. return string.Format("Select top {0} {1} from 
  136. {2} {3} ORDER BY ID" ,this.theParas.PageSize ,
  137. this.fieldStrs ,this.theParas.TableName ,
  138. this.theParas.WhereStr) ;
  139. }
  140. int innerCount = this.itemCount - 
  141. this.theParas.PageSize*pageIndex ;
  142. string innerSelStr = string.Format("Select 
  143. top {0} {1} from {2} {3} ORDER BY ID DESC " ,
  144. innerCount , this.fieldStrs ,this.theParas.
  145. TableName ,this.theParas.WhereStr) ;
  146. string outerSelStr = string.Format("Select top {0} 
  147. * from ({1}) DERIVEDTBL ORDER BY ID" ,this.
  148. theParas.PageSize ,innerSelStr) ;
  149. return outerSelStr ;
  150. }
  151. #region Initialize
  152. public void Initialize(IDBAccesser accesser, 
  153. int page_Size, string whereStr, string[] fields)
  154. {
  155. this.theParas = new DataPaginationParas(accesser.
  156. ConnectString ,accesser.DbTableName ,whereStr) ;
  157. this.theParas.Fields = fields ;
  158. this.theParas.PageSize = page_Size ;
  159. this.fieldStrs = this.theParas.GetFiedString() ; 
  160. this.adoBase = new SqlADOBase(this.theParas.
  161. ConnectString) ;
  162. } 
  163. public void Initialize(DataPaginationParas paras)
  164. {
  165. this.theParas = paras ;
  166. this.fieldStrs = this.theParas.GetFiedString() ; 
  167. this.adoBase = new SqlADOBase(this.theParas.
  168. ConnectString) ;
  169. }
  170. #endregion
  171. #endregion
  172. }

解这个类的实现,可以从GetPage(int index)方法入手,另外私有方法ConstrutSelectStr()的实现说明了如何使用嵌套sql语句进行随机分页搜索。

最后,关于分页管理器,需要指出的是,搜索对应的表必须有一个名为"ID"的主键--这是唯一的要求。另外,分页管理器实现用到的数据访问低阶封装IADOBase定义于EnterpriseServerBase类库中。

使用ASP.NET分页管理器是很简单的,加上UI界面后,只要把返回的DataTable绑定到DataGrid就可以了。


当前标题:ASP.NET分页管理器的设计及实现
网站网址:http://www.aqpgk.com/article/cdgodig.html