成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

ASP.NET分頁管理器的設計及實現

開發 后端
本文通過在DataGrid的web版控件中提供的自動分頁的功能實現ASP.NET分頁管理器,這款分頁管理器支持隨機跳轉和緩存。

在DataGrid的web版控件中提供了自動分頁的功能,但是我從來沒用過它,因為它實現的分頁只是一種假相。我們為什么需要分頁?那是因為符合條件的記錄可能很多,如果一次讀取所有的記錄,不僅延長獲取數據的時間,而且也極度浪費內存。而分頁的存在的主要目的正是為了解決這兩個問題(當然,也不排除為了UI美觀的需要而使用分頁的)。而web版的DataGrid是怎樣實現分頁的了?它并沒有打算解決上述兩個問題,而還是一次讀取所有的數據,然后以分頁的樣子表現出來。這是對效率和內存的極大損害!

于是我自己實現了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.  
  8. DataTable GetPage(int index) ; //取出第index頁  
  9. DataTable CurrentPage() ;  
  10. DataTable PrePage() ;  
  11. DataTable NextPage() ;  
  12.  
  13. int PageCount{get ;}  
  14. int CacherSize{get; set; }  

這個接口定義中,最主要的是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.  
  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語句中。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.  
  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 取出指定的一頁  
  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  

解這個類的實現,可以從GetPage(int index)方法入手,另外私有方法ConstrutSelectStr()的實現說明了如何使用嵌套sql語句進行隨機分頁搜索。

最后,關于分頁管理器,需要指出的是,搜索對應的表必須有一個名為"ID"的主鍵--這是唯一的要求。另外,分頁管理器實現用到的數據訪問低階封裝IADOBase定義于EnterpriseServerBase類庫中。

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

【編輯推薦】

  1. ASP.NET特點概述(1)
  2. ASP.NET多語言支持組件簡介
  3. ASP.NET服務器控件編程淺析
  4. ASP.NET移動開發入門基礎(1)
  5. ASP.NET SqlDataSource控件入門
責任編輯:冰荷 來源: yesky
相關推薦

2009-08-05 13:50:23

ASP.NET狀態管理

2009-08-14 13:37:25

ASP.NET靜態頁面

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高效分頁

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報表打印

2009-08-12 14:10:37

asp.net分頁代碼

2009-08-04 14:23:36

ASP.NET查詢分頁

2009-08-07 17:49:44

控件設計器

2009-08-10 14:08:15

ASP.NET服務器控ASP.NET組件設計

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組件設計

2009-08-05 16:59:55

ASP.NET組件設計

2009-08-10 10:19:47

ASP.NET組件設計

2009-08-03 14:15:24

ASP.NET系統用戶

2009-08-05 16:53:14

ASP.NET組件設計
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 狠狠躁天天躁夜夜躁婷婷老牛影视 | a级黄色片在线观看 | 日韩美女在线看免费观看 | 男女视频网站 | 色综合欧美 | 国产精品夜夜春夜夜爽久久电影 | 91综合在线视频 | 亚洲国产成人精品久久 | av一区在线观看 | av大片 | 一区二区三区在线免费 | 97国产精品 | 中文字幕成人 | 久久久久久久久久久国产 | 成人在线视频网站 | 亚洲综合一区二区三区 | 国产91综合 | 日韩欧美福利视频 | 一区二区在线不卡 | 成人午夜在线视频 | 97色在线视频 | 久久精品国产免费 | 91视频91| 2021天天干夜夜爽 | 国产婷婷精品 | 国产精品一区二 | 日韩中文字幕一区二区三区 | 日本又色又爽又黄又高潮 | 毛片免费观看 | 精品久久久久久久 | 婷婷综合久久 | 一级少妇女片 | 国产精品一区二区不卡 | 91欧美精品成人综合在线观看 | 欧美激情一区 | 国产精品不卡一区 | 人成精品 | 精品成人 | 一区二区三区欧美 | www九色 | 国产一区二区精 |