纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.5 KiB

3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace NFine.Data.Extensions
  9. {
  10. public class SqlServerHelper
  11. {
  12. #region 数据分页
  13. /// <summary>
  14. /// 摘要:
  15. /// 数据分页
  16. /// 参数:
  17. /// sql:传入要执行sql语句
  18. /// param:参数化
  19. /// orderField:排序字段
  20. /// orderType:排序类型
  21. /// pageIndex:当前页
  22. /// pageSize:页大小
  23. /// count:返回查询条数
  24. /// </summary>
  25. public static DataTable GetPageTable(string sql, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  26. {
  27. StringBuilder strSql = new StringBuilder();
  28. if (pageIndex == 0)
  29. {
  30. pageIndex = 1;
  31. }
  32. int num = (pageIndex - 1) * pageSize;
  33. int num1 = (pageIndex) * pageSize;
  34. string OrderBy = "";
  35. if (!string.IsNullOrEmpty(orderField))
  36. OrderBy = "Order By " + orderField + " " + orderType + "";
  37. else
  38. OrderBy = "order by (select 0)";
  39. strSql.Append("Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
  40. strSql.Append(" As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
  41. count = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, "Select Count(1) From (" + sql + ") As t", param));
  42. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), param);
  43. return DatabaseReader.ReaderToDataTable(dr);
  44. }
  45. /// <summary>
  46. /// 摘要:
  47. /// 数据分页
  48. /// 参数:
  49. /// sql:传入要执行sql语句
  50. /// orderField:排序字段
  51. /// orderType:排序类型
  52. /// pageIndex:当前页
  53. /// pageSize:页大小
  54. /// count:返回查询条数
  55. /// </summary>
  56. public static DataTable GetPageTable(string sql, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  57. {
  58. return GetPageTable(sql, null, orderField, orderType, pageIndex, pageSize, ref count);
  59. }
  60. /// <summary>
  61. /// 摘要:
  62. /// 数据分页
  63. /// 参数:
  64. /// sql:传入要执行sql语句
  65. /// param:参数化
  66. /// orderField:排序字段
  67. /// orderType:排序类型
  68. /// pageIndex:当前页
  69. /// pageSize:页大小
  70. /// count:返回查询条数
  71. /// </summary>
  72. public static List<T> GetPageList<T>(string sql, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  73. {
  74. StringBuilder strSql = new StringBuilder();
  75. if (pageIndex == 0)
  76. {
  77. pageIndex = 1;
  78. }
  79. int num = (pageIndex - 1) * pageSize;
  80. int num1 = (pageIndex) * pageSize;
  81. string OrderBy = "";
  82. if (!string.IsNullOrEmpty(orderField))
  83. OrderBy = "Order By " + orderField + " " + orderType + "";
  84. else
  85. OrderBy = "Order By (select 0)";
  86. strSql.Append("Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
  87. strSql.Append(" As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
  88. count = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, "Select Count(1) From (" + sql + ") As t", param));
  89. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), param);
  90. return DatabaseReader.ReaderToList<T>(dr);
  91. }
  92. /// <summary>
  93. /// 摘要:
  94. /// 数据分页
  95. /// 参数:
  96. /// sql:传入要执行sql语句
  97. /// orderField:排序字段
  98. /// orderType:排序类型
  99. /// pageIndex:当前页
  100. /// pageSize:页大小
  101. /// count:返回查询条数
  102. /// </summary>
  103. public static List<T> GetPageList<T>(string sql, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  104. {
  105. return GetPageList<T>(sql, null, orderField, orderType, pageIndex, pageSize, ref count);
  106. }
  107. #endregion
  108. }
  109. }