PageBase.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7. namespace DAL
  8. {
  9. /// <summary>
  10. /// 存储分页父类
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public static class PageBase<T> where T : new()
  14. {
  15. /// <summary>
  16. /// 分页查询
  17. /// </summary>
  18. /// <param name="t">类的对象</param>
  19. /// <param name="type">对象区分</param>
  20. /// <param name="tableName">查询的表名</param>
  21. /// <param name="fields">查询的字段名(全部字段为*)</param>
  22. /// <param name="orderField">排序字段(必须!支持多字段)</param>
  23. /// <param name="sqlWhere">条件语句(不用加where)</param>
  24. /// <param name="pageSize">每页多少条记录</param>
  25. /// <param name="pageIndex">指定当前为第几页</param>
  26. /// <param name="totalPage">返回总页数</param>
  27. /// <param name="totalRecord">返回总条数</param>
  28. /// <returns>返回集合</returns>
  29. public static List<T> excutePageSql(T t, string type, string tableName, string fields, string orderField, string sqlWhere, int pageSize, int pageIndex, out int totalPage, out int totalRecord, params SqlParameter[] param)
  30. {
  31. List<SqlParameter> spList = new List<SqlParameter>();
  32. spList.Add(new SqlParameter("@TableName", SqlDbType.VarChar));
  33. spList.Add(new SqlParameter("@Fields", SqlDbType.VarChar));
  34. spList.Add(new SqlParameter("@OrderField", SqlDbType.VarChar));
  35. spList.Add(new SqlParameter("@sqlWhere", SqlDbType.VarChar));
  36. spList.Add(new SqlParameter("@pageSize", SqlDbType.Int));
  37. spList.Add(new SqlParameter("@pageIndex", SqlDbType.Int));
  38. spList.Add(new SqlParameter("@TotalPage", SqlDbType.Int));
  39. spList.Add(new SqlParameter("@Record", SqlDbType.Int));
  40. spList[0].Value = tableName;
  41. spList[1].Value = fields;
  42. spList[2].Value = orderField;
  43. spList[3].Value = sqlWhere;
  44. spList[4].Value = pageSize;
  45. spList[5].Value = pageIndex;
  46. spList[6].Direction = ParameterDirection.Output;
  47. spList[7].Direction = ParameterDirection.Output;
  48. List<T> list = ServiceBase<T>.excuteSql(new T(), type, "exec_page", CommandType.StoredProcedure, spList.ToArray());
  49. //获取总页数
  50. try
  51. {
  52. totalPage = Convert.ToInt32(spList[6].Value);
  53. }
  54. catch
  55. {
  56. totalPage = 0;
  57. }
  58. try
  59. {
  60. totalRecord = Convert.ToInt32(spList[7].Value);
  61. }
  62. catch
  63. {
  64. totalRecord = 0;
  65. }
  66. return list;
  67. }
  68. }
  69. }