日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NET分頁管理器的設(shè)計及實現(xiàn)

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

成都創(chuàng)新互聯(lián)公司是專業(yè)的平陽網(wǎng)站建設(shè)公司,平陽接單;提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行平陽網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

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

(1)支持隨機跳轉(zhuǎn)。這是通過嵌套Select語句實現(xiàn)的。

(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()方法,實現(xiàn)了這個方法,其它的三個獲取頁面的方法CurrentPage、PrePage、NextPage也就非常容易了。另外,CacherSize屬性可以讓我們指定緩存頁面的數(shù)量。如果不需要緩存,則設(shè)置其值<=0,如果需要無限緩存,則值為Int.MaxValue。

IPaginationManager接口中的第二個Initialize方法,你不要關(guān)心,它是給XCodeFactory生成的數(shù)據(jù)層使用了,我們來看看第一個Initialize方法的參數(shù)類型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. #region GetFiedString
  18. public string GetFiedString()
  19. {
  20. if(this.Fields == null) 
  21. {
  22. this.Fields = new string[] {"*"} ;
  23. }
  24. string fieldStrs = "" ;
  25. for(int i=0 ;i
  26. {
  27. fieldStrs += " " + this.Fields[i] ;
  28. if(i != (this.Fields.Length -1))
  29. {
  30. fieldStrs += " , " ;
  31. }
  32. else
  33. {
  34. fieldStrs += " " ;
  35. }
  36. }
  37. return fieldStrs ;
  38. }
  39. #endregion
  40. }

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

現(xiàn)在來看看ASP.NET分頁管理器的實現(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. 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. public PaginationManager()
  31. {}
  32. #region IDataPaginationManager 成員
  33. public int CacherSize
  34. {
  35. get
  36. {
  37. if(this.fixCacher == null)
  38. {
  39. return 0 ;
  40. }
  41. return this.fixCacher.Size ;
  42. }
  43. set
  44. {
  45. if(this.fixCacher == null)
  46. {
  47. this.fixCacher = new FixCacher(value) ;
  48. }
  49. else
  50. {
  51. this.fixCacher.Size = value ;
  52. }
  53. }
  54. }
  55. public int PageCount
  56. {
  57. get
  58. {
  59. if(this.pageCount == -1)
  60. {
  61. string selCountStr = string.Format
  62. ("Select count(*) from {0} {1}" ,this.theParas.
  63. TableName ,this.theParas.WhereStr) ;
  64. DataSet ds = this.adoBase.DoQuery(selCountStr) ;
  65. this.itemCount = int.Parse(ds.Tables[0].
  66. Rows[0][0].ToString()) ;
  67. this.pageCount = this.itemCount/this.
  68. theParas.PageSize ;
  69. if((this.itemCount%this.theParas.PageSize > 0))
  70. {
  71. ++ this.pageCount ;
  72. }
  73. }
  74. return this.pageCount ;
  75. }
  76. }
  77. /// 
  78. /// GetPage 取出指定的一頁
  79. ///
  80.    
  81. public DataTable GetPage(int index)
  82. {
  83. if(index == this.curPageIndex)
  84. {
  85. return this.curPage ;
  86. }
  87. if((index < 0) || (index > (this.PageCount-1)))
  88. {
  89. return null;
  90. }
  91. DataTable dt = this.GetCachedObject(index) ;
  92. if(dt == null)
  93. {
  94. string selectStr = this.ConstrutSelectStr(index) ;
  95. DataSet ds = this.adoBase.DoQuery(selectStr) ;
  96. dt = ds.Tables[0] ;
  97. this.CacheObject(index ,dt) ;
  98. }
  99. this.curPage = dt ;
  100. this.curPageIndex = index ;
  101. return this.curPage ;
  102. }
  103. private DataTable GetCachedObject(int index)
  104. {
  105. if(this.fixCacher == null)
  106. {
  107. return null ;
  108. }
  109. return (DataTable)this.fixCacher[index] ;
  110. }
  111. private void CacheObject(int index ,DataTable page)
  112. {
  113. if(this.fixCacher != null)
  114. {
  115. this.fixCacher.PutIn(index ,page) ;
  116. }
  117. }
  118. public DataTable CurrentPage()
  119. {
  120. return this.curPage ;
  121. }
  122. public DataTable PrePage()
  123. {
  124. return this.GetPage((--this.curPageIndex)) ;
  125. }
  126. public DataTable NextPage()
  127. {
  128. return this.GetPage((++this.curPageIndex)) ;
  129. private string ConstrutSelectStr(int pageIndex)
  130. {
  131. if(pageIndex == 0)
  132. {
  133. return string.Format("Select top {0} {1} from 
  134. {2} {3} ORDER BY ID" ,this.theParas.PageSize ,
  135. this.fieldStrs ,this.theParas.TableName ,
  136. this.theParas.WhereStr) ;
  137. }
  138. int innerCount = this.itemCount - 
  139. this.theParas.PageSize*pageIndex ;
  140. string innerSelStr = string.Format("Select 
  141. top {0} {1} from {2} {3} ORDER BY ID DESC " ,
  142. innerCount , this.fieldStrs ,this.theParas.
  143. TableName ,this.theParas.WhereStr) ;
  144. string outerSelStr = string.Format("Select top {0} 
  145. * from ({1}) DERIVEDTBL ORDER BY ID" ,this.
  146. theParas.PageSize ,innerSelStr) ;
  147. return outerSelStr ;
  148. }
  149. #region Initialize
  150. public void Initialize(IDBAccesser accesser, 
  151. int page_Size, string whereStr, string[] fields)
  152. {
  153. this.theParas = new DataPaginationParas(accesser.
  154. ConnectString ,accesser.DbTableName ,whereStr) ;
  155. this.theParas.Fields = fields ;
  156. this.theParas.PageSize = page_Size ;
  157. this.fieldStrs = this.theParas.GetFiedString() ; 
  158. this.adoBase = new SqlADOBase(this.theParas.
  159. ConnectString) ;
  160. public void Initialize(DataPaginationParas paras)
  161. {
  162. this.theParas = paras ;
  163. this.fieldStrs = this.theParas.GetFiedString() ; 
  164. this.adoBase = new SqlADOBase(this.theParas.
  165. ConnectString) ;
  166. }
  167. #endregion
  168. #endregion
  169. }

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

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

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


網(wǎng)站題目:ASP.NET分頁管理器的設(shè)計及實現(xiàn)
文章URL:http://www.dlmjj.cn/article/cdgodig.html