自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET分頁(yè)管理器的設(shè)計(jì)及實(shí)現(xiàn)

開發(fā) 后端
本文通過(guò)在DataGrid的web版控件中提供的自動(dòng)分頁(yè)的功能實(shí)現(xiàn)ASP.NET分頁(yè)管理器,這款分頁(yè)管理器支持隨機(jī)跳轉(zhuǎn)和緩存。

在DataGrid的web版控件中提供了自動(dòng)分頁(yè)的功能,但是我從來(lái)沒用過(guò)它,因?yàn)樗鼘?shí)現(xiàn)的分頁(yè)只是一種假相。我們?yōu)槭裁葱枰猪?yè)?那是因?yàn)榉蠗l件的記錄可能很多,如果一次讀取所有的記錄,不僅延長(zhǎng)獲取數(shù)據(jù)的時(shí)間,而且也極度浪費(fèi)內(nèi)存。而分頁(yè)的存在的主要目的正是為了解決這兩個(gè)問題(當(dāng)然,也不排除為了UI美觀的需要而使用分頁(yè)的)。而web版的DataGrid是怎樣實(shí)現(xiàn)分頁(yè)的了?它并沒有打算解決上述兩個(gè)問題,而還是一次讀取所有的數(shù)據(jù),然后以分頁(yè)的樣子表現(xiàn)出來(lái)。這是對(duì)效率和內(nèi)存的極大損害!

于是我自己實(shí)現(xiàn)了ASP.NET分頁(yè)管理器IPaginationManager ,IPaginationManager 每次從數(shù)據(jù)庫(kù)中讀取指定的任意一頁(yè),并且可以緩存指定數(shù)量的page。這個(gè)分頁(yè)管理器的主要特點(diǎn)是:

(1)支持隨機(jī)跳轉(zhuǎn)。這是通過(guò)嵌套Select語(yǔ)句實(shí)現(xiàn)的。

(2)支持緩存。通過(guò)EnterpriseServerBase.DataStructure.FixCacher進(jìn)行支持。

先來(lái)看看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.  
  8. DataTable GetPage(int index) ; //取出第index頁(yè)  
  9. DataTable CurrentPage() ;  
  10. DataTable PrePage() ;  
  11. DataTable NextPage() ;  
  12.  
  13. int PageCount{get ;}  
  14. int CacherSize{get; set; }  

這個(gè)接口定義中,最主要的是GetPage()方法,實(shí)現(xiàn)了這個(gè)方法,其它的三個(gè)獲取頁(yè)面的方法CurrentPage、PrePage、NextPage也就非常容易了。另外,CacherSize屬性可以讓我們指定緩存頁(yè)面的數(shù)量。如果不需要緩存,則設(shè)置其值<=0,如果需要無(wú)限緩存,則值為Int.MaxValue。

IPaginationManager接口中的第二個(gè)Initialize方法,你不要關(guān)心,它是給XCodeFactory生成的數(shù)據(jù)層使用了,我們來(lái)看看第一個(gè)Initialize方法的參數(shù)類型DataPaginationParas的定義:

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

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

現(xiàn)在來(lái)看看ASP.NET分頁(yè)管理器的實(shí)現(xiàn)了:

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

解這個(gè)類的實(shí)現(xiàn),可以從GetPage(int index)方法入手,另外私有方法ConstrutSelectStr()的實(shí)現(xiàn)說(shuō)明了如何使用嵌套sql語(yǔ)句進(jìn)行隨機(jī)分頁(yè)搜索。

最后,關(guān)于分頁(yè)管理器,需要指出的是,搜索對(duì)應(yīng)的表必須有一個(gè)名為"ID"的主鍵--這是唯一的要求。另外,分頁(yè)管理器實(shí)現(xiàn)用到的數(shù)據(jù)訪問低階封裝IADOBase定義于EnterpriseServerBase類庫(kù)中。

使用ASP.NET分頁(yè)管理器是很簡(jiǎn)單的,加上UI界面后,只要把返回的DataTable綁定到DataGrid就可以了。

【編輯推薦】

  1. ASP.NET特點(diǎn)概述(1)
  2. ASP.NET多語(yǔ)言支持組件簡(jiǎn)介
  3. ASP.NET服務(wù)器控件編程淺析
  4. ASP.NET移動(dòng)開發(fā)入門基礎(chǔ)(1)
  5. ASP.NET SqlDataSource控件入門
責(zé)任編輯:冰荷 來(lái)源: yesky
相關(guān)推薦

2009-08-05 13:50:23

ASP.NET狀態(tài)管理

2009-08-14 13:37:25

ASP.NET靜態(tài)頁(yè)面

2009-07-28 14:47:18

ASP.NET MVC

2009-08-04 14:18:49

ASP.NET郵件列表

2009-11-06 09:23:41

ASP.NET高效分頁(yè)

2009-09-10 09:50:47

ASP.NET MVC

2012-04-23 15:10:18

ASP.NET

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-08-12 18:19:46

ASP.NET報(bào)表打印

2009-08-12 14:10:37

asp.net分頁(yè)代碼

2009-08-04 14:23:36

ASP.NET查詢分頁(yè)

2009-08-07 17:49:44

控件設(shè)計(jì)器

2009-08-10 14:08:15

ASP.NET服務(wù)器控ASP.NET組件設(shè)計(jì)

2012-04-13 10:05:24

ASP.NET

2010-03-19 09:17:16

ASP.NET MVC

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2009-08-05 16:59:55

ASP.NET組件設(shè)計(jì)

2009-08-03 14:15:24

ASP.NET系統(tǒng)用戶

2009-08-05 16:53:14

ASP.NET組件設(shè)計(jì)

2009-08-10 10:19:47

ASP.NET組件設(shè)計(jì)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)