Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

133 linhas
5.8 KiB

3 anos atrás
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Basic
  5. {
  6. /// <summary>
  7. /// 双TOP二分法生成分页SQL类(支持MSSQL、ACCESS)
  8. /// </summary>
  9. public static class PagingHelper
  10. {
  11. /// <summary>
  12. /// 获取分页SQL语句,排序字段需要构成唯一记录
  13. /// </summary>
  14. /// <param name="_recordCount">记录总数</param>
  15. /// <param name="_pageSize">每页记录数</param>
  16. /// <param name="_pageIndex">当前页数</param>
  17. /// <param name="_safeSql">SQL查询语句</param>
  18. /// <param name="_orderField">排序字段,多个则用“,”隔开</param>
  19. /// <returns>分页SQL语句</returns>
  20. public static string CreatePagingSql(int _recordCount, int _pageSize, int _pageIndex, string _safeSql, string _orderField)
  21. {
  22. //重新组合排序字段,防止有错误
  23. string[] arrStrOrders = _orderField.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  24. StringBuilder sbOriginalOrder = new StringBuilder(); //原排序字段
  25. StringBuilder sbReverseOrder = new StringBuilder(); //与原排序字段相反,用于分页
  26. for (int i = 0; i < arrStrOrders.Length; i++)
  27. {
  28. arrStrOrders[i] = arrStrOrders[i].Trim(); //去除前后空格
  29. if (i != 0)
  30. {
  31. sbOriginalOrder.Append(", ");
  32. sbReverseOrder.Append(", ");
  33. }
  34. sbOriginalOrder.Append(arrStrOrders[i]);
  35. int index = arrStrOrders[i].IndexOf(" "); //判断是否有升降标识
  36. if (index > 0)
  37. {
  38. //替换升降标识,分页所需
  39. bool flag = arrStrOrders[i].IndexOf(" DESC", StringComparison.OrdinalIgnoreCase) != -1;
  40. sbReverseOrder.AppendFormat("{0} {1}", arrStrOrders[i].Remove(index), flag ? "ASC" : "DESC");
  41. }
  42. else
  43. {
  44. sbReverseOrder.AppendFormat("{0} DESC", arrStrOrders[i]);
  45. }
  46. }
  47. //计算总页数
  48. _pageSize = _pageSize == 0 ? _recordCount : _pageSize;
  49. int pageCount = (_recordCount + _pageSize - 1) / _pageSize;
  50. //检查当前页数
  51. if (_pageIndex < 1)
  52. {
  53. _pageIndex = 1;
  54. }
  55. else if (_pageIndex > pageCount)
  56. {
  57. _pageIndex = pageCount;
  58. }
  59. StringBuilder sbSql = new StringBuilder();
  60. //第一页时,直接使用TOP n,而不进行分页查询
  61. if (_pageIndex == 1)
  62. {
  63. sbSql.AppendFormat(" SELECT TOP {0} * ", _pageSize);
  64. sbSql.AppendFormat(" FROM ({0}) AS T ", _safeSql);
  65. sbSql.AppendFormat(" ORDER BY {0} ", sbOriginalOrder.ToString());
  66. }
  67. //最后一页时,减少一个TOP
  68. else if (_pageIndex == pageCount)
  69. {
  70. sbSql.Append(" SELECT * FROM ");
  71. sbSql.Append(" ( ");
  72. sbSql.AppendFormat(" SELECT TOP {0} * ", _recordCount - _pageSize * (_pageIndex - 1));
  73. sbSql.AppendFormat(" FROM ({0}) AS T ", _safeSql);
  74. sbSql.AppendFormat(" ORDER BY {0} ", sbReverseOrder.ToString());
  75. sbSql.Append(" ) AS T ");
  76. sbSql.AppendFormat(" ORDER BY {0} ", sbOriginalOrder.ToString());
  77. }
  78. //前半页数时的分页
  79. else if (_pageIndex <= (pageCount / 2 + pageCount % 2) + 1)
  80. {
  81. sbSql.Append(" SELECT * FROM ");
  82. sbSql.Append(" ( ");
  83. sbSql.AppendFormat(" SELECT TOP {0} * FROM ", _pageSize);
  84. sbSql.Append(" ( ");
  85. sbSql.AppendFormat(" SELECT TOP {0} * ", _pageSize * _pageIndex);
  86. sbSql.AppendFormat(" FROM ({0}) AS T ", _safeSql);
  87. sbSql.AppendFormat(" ORDER BY {0} ", sbOriginalOrder.ToString());
  88. sbSql.Append(" ) AS T ");
  89. sbSql.AppendFormat(" ORDER BY {0} ", sbReverseOrder.ToString());
  90. sbSql.Append(" ) AS T ");
  91. sbSql.AppendFormat(" ORDER BY {0} ", sbOriginalOrder.ToString());
  92. }
  93. //后半页数时的分页
  94. else
  95. {
  96. sbSql.AppendFormat(" SELECT TOP {0} * FROM ", _pageSize);
  97. sbSql.Append(" ( ");
  98. sbSql.AppendFormat(" SELECT TOP {0} * ", ((_recordCount % _pageSize) + _pageSize * (pageCount - _pageIndex) + 1));
  99. sbSql.AppendFormat(" FROM ({0}) AS T ", _safeSql);
  100. sbSql.AppendFormat(" ORDER BY {0} ", sbReverseOrder.ToString());
  101. sbSql.Append(" ) AS T ");
  102. sbSql.AppendFormat(" ORDER BY {0} ", sbOriginalOrder.ToString());
  103. }
  104. return sbSql.ToString();
  105. }
  106. /// <summary>
  107. /// 获取记录总数SQL语句
  108. /// </summary>
  109. /// <param name="_n">限定记录数</param>
  110. /// <param name="_safeSql">SQL查询语句</param>
  111. /// <returns>记录总数SQL语句</returns>
  112. public static string CreateTopnSql(int _n, string _safeSql)
  113. {
  114. return string.Format(" SELECT TOP {0} * FROM ({1}) AS T ", _n, _safeSql);
  115. }
  116. /// <summary>
  117. /// 获取记录总数SQL语句
  118. /// </summary>
  119. /// <param name="_safeSql">SQL查询语句</param>
  120. /// <returns>记录总数SQL语句</returns>
  121. public static string CreateCountingSql(string _safeSql)
  122. {
  123. return string.Format(" SELECT COUNT(1) AS RecordCount FROM ({0}) AS T ", _safeSql);
  124. }
  125. }
  126. }