MakePageSql.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ant.Service.Utility
  7. {
  8. public class MakePageSql
  9. {
  10. public static StringBuilder StrShowSql = new StringBuilder(); //组装外显的SQL语句
  11. /// <summary>
  12. /// 拼接分页查询SQL
  13. /// </summary>
  14. /// <param name="orderByName"></param>
  15. /// <param name="columnName"></param>
  16. /// <param name="joinStr"></param>
  17. /// <param name="strWhere"></param>
  18. /// <param name="page"></param>
  19. /// <param name="pagesize"></param>
  20. /// <returns></returns>
  21. public static string GetPageSql(string orderByName, string columnName, string joinStr, string strWhere, int page, int pagesize)
  22. {
  23. StrShowSql = new StringBuilder();
  24. StrShowSql.AppendFormat("SELECT * FROM (SELECT ROW_NUMBER() OVER (order by {0} ) AS rowNumber, {1} ", orderByName, (columnName.IsEmpty()) ? "*" : columnName);
  25. StrShowSql.AppendFormat(" from {0} ", joinStr);
  26. if (!strWhere.IsEmpty())
  27. StrShowSql.AppendFormat(" where 1=1 and {0}", strWhere);
  28. StrShowSql.AppendFormat(") AS sp WHERE rowNumber BETWEEN {0} and {1} ", (((page - 1) * pagesize) + 1), (page * pagesize));
  29. return StrShowSql.ToString();
  30. }
  31. /// <summary>
  32. /// 获取分页统计记录的SQL语句
  33. /// </summary>
  34. /// <param name="joinStr"></param>
  35. /// <param name="strWhere"></param>
  36. /// <returns></returns>
  37. public static string GetPageCount(string joinStr, string strWhere)
  38. {
  39. StrShowSql = new StringBuilder();
  40. StrShowSql.AppendFormat(" SELECT count(1) from {0} ", joinStr);
  41. if (!strWhere.IsEmpty())
  42. StrShowSql.AppendFormat(" where 1=1 and {0}", strWhere);
  43. return StrShowSql.ToString();
  44. }
  45. }
  46. }