PagerHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ProjectBase.Data.Pager
  7. {
  8. /// <summary>
  9. /// 根据各种不同数据库生成不同分页语句的辅助类 PagerHelper
  10. /// </summary>
  11. public class PagerHelper
  12. {
  13. #region 成员变量
  14. private string tableName;//待查询表或自定义查询语句
  15. private string fieldsToReturn = "*";//需要返回的列
  16. private string fieldNameToSort = string.Empty;//排序字段名称
  17. private int pageSize = 10;//页尺寸,就是一页显示多少条记录
  18. private int pageIndex = 1;//当前的页码
  19. private bool isDescending = false;//是否以降序排列
  20. private string strwhere = string.Empty;//检索条件(注意: 不要加 where)
  21. #endregion
  22. #region 属性对象
  23. /// <summary>
  24. /// 待查询表或自定义查询语句
  25. /// </summary>
  26. public string TableName
  27. {
  28. get { return tableName; }
  29. set { tableName = value; }
  30. }
  31. /// <summary>
  32. /// 需要返回的列
  33. /// </summary>
  34. public string FieldsToReturn
  35. {
  36. get { return fieldsToReturn; }
  37. set { fieldsToReturn = value; }
  38. }
  39. /// <summary>
  40. /// 排序字段名称
  41. /// </summary>
  42. public string FieldNameToSort
  43. {
  44. get { return fieldNameToSort; }
  45. set { fieldNameToSort = value; }
  46. }
  47. /// <summary>
  48. /// 页尺寸,就是一页显示多少条记录
  49. /// </summary>
  50. public int PageSize
  51. {
  52. get { return pageSize; }
  53. set { pageSize = value; }
  54. }
  55. /// <summary>
  56. /// 当前的页码
  57. /// </summary>
  58. public int PageIndex
  59. {
  60. get { return pageIndex; }
  61. set { pageIndex = value; }
  62. }
  63. /// <summary>
  64. /// 是否以降序排列结果
  65. /// </summary>
  66. public bool IsDescending
  67. {
  68. get { return isDescending; }
  69. set { isDescending = value; }
  70. }
  71. /// <summary>
  72. /// 检索条件(注意: 不要加 where)
  73. /// </summary>
  74. public string StrWhere
  75. {
  76. get { return strwhere; }
  77. set { strwhere = value; }
  78. }
  79. /// <summary>
  80. /// 表或Sql语句包装属性
  81. /// </summary>
  82. internal string TableOrSqlWrapper
  83. {
  84. get
  85. {
  86. bool isSql = tableName.ToLower().Contains("from");
  87. if (isSql)
  88. {
  89. return string.Format("({0}) AA ", tableName);//如果是Sql语句,则加括号后再使用
  90. }
  91. else
  92. {
  93. return tableName;//如果是表名,则直接使用
  94. }
  95. }
  96. }
  97. #endregion
  98. #region 构造函数
  99. /// <summary>
  100. /// 默认构造函数,其他通过属性设置
  101. /// </summary>
  102. public PagerHelper()
  103. {
  104. }
  105. /// <summary>
  106. /// 完整的构造函数,可以包含条件,返回记录字段等条件
  107. /// </summary>
  108. /// <param name="tableName">自定义查询语句</param>
  109. /// <param name="fieldsToReturn">需要返回的列</param>
  110. /// <param name="fieldNameToSort">排序字段名称</param>
  111. /// <param name="pageSize">页尺寸</param>
  112. /// <param name="pageIndex">当前的页码</param>
  113. /// <param name="isDescending">是否以降序排列</param>
  114. /// <param name="strwhere">检索条件</param>
  115. /// <param name="connectionString">连接字符串</param>
  116. public PagerHelper(string tableName, string fieldsToReturn, string fieldNameToSort,
  117. int pageSize, int pageIndex, bool isDescending, string strwhere)
  118. {
  119. this.tableName = tableName;
  120. this.fieldsToReturn = fieldsToReturn;
  121. this.fieldNameToSort = fieldNameToSort;
  122. this.pageSize = pageSize;
  123. this.pageIndex = pageIndex;
  124. this.isDescending = isDescending;
  125. this.strwhere = strwhere;
  126. }
  127. #endregion
  128. /// <summary>
  129. /// 不依赖于存储过程的分页(Oracle)
  130. /// </summary>
  131. /// <param name="isDoCount">如果isDoCount为True,返回总数统计Sql;否则返回分页语句Sql</param>
  132. /// <returns></returns>
  133. private string GetOracleSql(bool isDoCount)
  134. {
  135. string sql = "";
  136. if (string.IsNullOrEmpty(this.strwhere))
  137. {
  138. this.strwhere = " (1=1) ";
  139. }
  140. if (isDoCount)//执行总数统计
  141. {
  142. sql = string.Format("select count(*) as Total from {0} Where {1} ", this.TableOrSqlWrapper, this.strwhere);
  143. }
  144. else
  145. {
  146. string strOrder = string.Format(" order by {0} {1}", this.fieldNameToSort, this.isDescending ? "DESC" : "ASC");
  147. int minRow = pageSize * (pageIndex - 1);
  148. int maxRow = pageSize * pageIndex;
  149. string selectSql = string.Format("select {0} from {1} Where {2} {3}", fieldsToReturn, this.TableOrSqlWrapper, this.strwhere, strOrder);
  150. sql = string.Format(@"select b.* from
  151. (select a.*, rownum as rowIndex from({2}) a) b
  152. where b.rowIndex > {0} and b.rowIndex <= {1}", minRow, maxRow, selectSql);
  153. }
  154. return sql;
  155. }
  156. /// <summary>
  157. /// 不依赖于存储过程的分页(SqlServer)
  158. /// </summary>
  159. /// <param name="isDoCount">如果isDoCount为True,返回总数统计Sql;否则返回分页语句Sql</param>
  160. /// <returns></returns>
  161. private string GetSqlServerSql(bool isDoCount)
  162. {
  163. string sql = "";
  164. if (string.IsNullOrEmpty(this.strwhere))
  165. {
  166. this.strwhere = " (1=1) ";
  167. }
  168. if (isDoCount)//执行总数统计
  169. {
  170. sql = string.Format("select count(*) as Total from {0} Where {1} ", this.TableOrSqlWrapper, this.strwhere);
  171. }
  172. else
  173. {
  174. // With Paging AS
  175. // ( SELECT ROW_NUMBER() OVER (order by SortCode desc) as RowNumber, * FROM T_ACL_User )
  176. // SELECT * from Paging Where RowNumber Between 1 and 20
  177. string strOrder = string.Format(" order by {0} {1}", this.fieldNameToSort, this.isDescending ? "DESC" : "ASC");
  178. int minRow = pageSize * (pageIndex - 1) + 1;
  179. int maxRow = pageSize * pageIndex;
  180. sql = string.Format(@"With Paging AS
  181. ( SELECT ROW_NUMBER() OVER ({0}) as RowNumber, {1} FROM {2} Where {3})
  182. SELECT * FROM Paging WHERE RowNumber Between {4} and {5}", strOrder, this.fieldsToReturn, this.TableOrSqlWrapper, this.strwhere,
  183. minRow, maxRow);
  184. }
  185. return sql;
  186. }
  187. /// <summary>
  188. /// 不依赖于存储过程的分页(Access)
  189. /// </summary>
  190. /// <param name="isDoCount">如果isDoCount为True,返回总数统计Sql;否则返回分页语句Sql</param>
  191. /// <returns></returns>
  192. private string GetAccessSql(bool isDoCount)
  193. {
  194. string sql = "";
  195. if (string.IsNullOrEmpty(this.strwhere))
  196. {
  197. this.strwhere = " (1=1) ";
  198. }
  199. if (isDoCount)//执行总数统计
  200. {
  201. sql = string.Format("select count(*) as Total from {0} Where {1} ", this.TableOrSqlWrapper, this.strwhere);
  202. }
  203. else
  204. {
  205. string strTemp = string.Empty;
  206. string strOrder = string.Empty;
  207. if (this.isDescending)
  208. {
  209. strTemp = "<(select min";
  210. strOrder = string.Format(" order by [{0}] desc", this.fieldNameToSort);
  211. }
  212. else
  213. {
  214. strTemp = ">(select max";
  215. strOrder = string.Format(" order by [{0}] asc", this.fieldNameToSort);
  216. }
  217. sql = string.Format("select top {0} {1} from {2} ", this.pageSize, this.fieldsToReturn, this.TableOrSqlWrapper);
  218. //如果是第一页就执行以上代码,这样会加快执行速度
  219. if (this.pageIndex == 1)
  220. {
  221. sql += string.Format(" Where {0} ", this.strwhere);
  222. sql += strOrder;
  223. }
  224. else
  225. {
  226. sql += string.Format(" Where [{0}] {1} ([{0}]) from (select top {2} [{0}] from {3} where {5} {4} ) as tblTmp) and {5} {4}",
  227. this.fieldNameToSort, strTemp, (this.pageIndex - 1) * this.pageSize, this.TableOrSqlWrapper, strOrder, this.strwhere);
  228. }
  229. }
  230. return sql;
  231. }
  232. /// <summary>
  233. /// 不依赖于存储过程的分页(MySql)
  234. /// </summary>
  235. /// <param name="isDoCount">如果isDoCount为True,返回总数统计Sql;否则返回分页语句Sql</param>
  236. /// <returns></returns>
  237. private string GetMySqlSql(bool isDoCount)
  238. {
  239. string sql = "";
  240. if (string.IsNullOrEmpty(this.strwhere))
  241. {
  242. this.strwhere = " (1=1) ";
  243. }
  244. if (isDoCount)//执行总数统计
  245. {
  246. sql = string.Format("select count(*) as Total from {0} Where {1} ", this.TableOrSqlWrapper, this.strwhere);
  247. }
  248. else
  249. {
  250. //SELECT * FROM 表名称 LIMIT M,N
  251. string strOrder = string.Format(" order by {0} {1}", this.fieldNameToSort, this.isDescending ? "DESC" : "ASC");
  252. int minRow = pageSize * (pageIndex - 1);
  253. int maxRow = pageSize * pageIndex;
  254. sql = string.Format("select {0} from {1} Where {2} {3} LIMIT {4},{5}",
  255. fieldsToReturn, this.TableOrSqlWrapper, this.strwhere, strOrder, minRow, maxRow);
  256. }
  257. return sql;
  258. }
  259. /// <summary>
  260. /// 不依赖于存储过程的分页(SQLite)
  261. /// </summary>
  262. /// <param name="isDoCount">如果isDoCount为True,返回总数统计Sql;否则返回分页语句Sql</param>
  263. /// <returns></returns>
  264. private string GetSQLiteSql(bool isDoCount)
  265. {
  266. string sql = "";
  267. if (string.IsNullOrEmpty(this.strwhere))
  268. {
  269. this.strwhere = " (1=1) ";
  270. }
  271. if (isDoCount)//执行总数统计
  272. {
  273. sql = string.Format("select count(*) as Total from {0} Where {1} ", this.TableOrSqlWrapper, this.strwhere);
  274. }
  275. else
  276. {
  277. //SELECT * FROM 表名称 LIMIT M,N
  278. string strOrder = string.Format(" order by {0} {1}", this.fieldNameToSort, this.isDescending ? "DESC" : "ASC");
  279. int minRow = pageSize * (pageIndex - 1);
  280. int maxRow = pageSize * pageIndex;
  281. sql = string.Format("select {0} from {1} Where {2} {3} LIMIT {4},{5}",
  282. fieldsToReturn, this.TableOrSqlWrapper, this.strwhere, strOrder, minRow, maxRow);
  283. }
  284. return sql;
  285. }
  286. /// <summary>
  287. /// 获取对应数据库的分页语句(指定数据库类型)
  288. /// </summary>
  289. /// <param name="dbType">数据库类型枚举</param>
  290. /// <param name="isDoCount">如果isDoCount为True,返回总数统计Sql;否则返回分页语句Sql</param>
  291. public string GetPagingSql(DatabaseType dbType, bool isDoCount)
  292. {
  293. string sql = "";
  294. switch (dbType)
  295. {
  296. case DatabaseType.Access:
  297. sql = GetAccessSql(isDoCount);
  298. break;
  299. case DatabaseType.SqlServer:
  300. sql = GetSqlServerSql(isDoCount);
  301. break;
  302. case DatabaseType.Oracle:
  303. sql = GetOracleSql(isDoCount);
  304. break;
  305. case DatabaseType.MySql:
  306. sql = GetMySqlSql(isDoCount);
  307. break;
  308. case DatabaseType.SQLite:
  309. sql = GetSQLiteSql(isDoCount);
  310. break;
  311. }
  312. return sql;
  313. }
  314. /// <summary>
  315. /// 获取对应数据库的分页语句(从配置文件读取数据库类型:ComponentDbType)
  316. /// </summary>
  317. /// <param name="dbType">数据库类型枚举</param>
  318. /// <param name="isDoCount">如果isDoCount为True,返回总数统计Sql;否则返回分页语句Sql</param>
  319. public string GetPagingSql(bool isDoCount)
  320. {
  321. //AppConfig config = new AppConfig();
  322. string databaseType = System.Configuration.ConfigurationManager.AppSettings["ComponentDbType"];
  323. DatabaseType dbType = GetDataBaseType(databaseType);
  324. return GetPagingSql(dbType, isDoCount);
  325. }
  326. private DatabaseType GetDataBaseType(string databaseType)
  327. {
  328. DatabaseType returnValue = DatabaseType.SqlServer;
  329. foreach (DatabaseType dbType in Enum.GetValues(typeof(DatabaseType)))
  330. {
  331. if (dbType.ToString().Equals(databaseType, StringComparison.OrdinalIgnoreCase))
  332. {
  333. returnValue = dbType;
  334. break;
  335. }
  336. }
  337. return returnValue;
  338. }
  339. }
  340. /// <summary>
  341. /// 数据库类型
  342. /// </summary>
  343. public enum DatabaseType { SqlServer, Oracle, Access, MySql, SQLite }
  344. }