1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- using System.Data;
- namespace DAL
- {
- /// <summary>
- /// 存储分页父类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public static class PageBase<T> where T : new()
- {
- /// <summary>
- /// 分页查询
- /// </summary>
- /// <param name="t">类的对象</param>
- /// <param name="type">对象区分</param>
- /// <param name="tableName">查询的表名</param>
- /// <param name="fields">查询的字段名(全部字段为*)</param>
- /// <param name="orderField">排序字段(必须!支持多字段)</param>
- /// <param name="sqlWhere">条件语句(不用加where)</param>
- /// <param name="pageSize">每页多少条记录</param>
- /// <param name="pageIndex">指定当前为第几页</param>
- /// <param name="totalPage">返回总页数</param>
- /// <param name="totalRecord">返回总条数</param>
- /// <returns>返回集合</returns>
- 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)
- {
- List<SqlParameter> spList = new List<SqlParameter>();
- spList.Add(new SqlParameter("@TableName", SqlDbType.VarChar));
- spList.Add(new SqlParameter("@Fields", SqlDbType.VarChar));
- spList.Add(new SqlParameter("@OrderField", SqlDbType.VarChar));
- spList.Add(new SqlParameter("@sqlWhere", SqlDbType.VarChar));
- spList.Add(new SqlParameter("@pageSize", SqlDbType.Int));
- spList.Add(new SqlParameter("@pageIndex", SqlDbType.Int));
- spList.Add(new SqlParameter("@TotalPage", SqlDbType.Int));
- spList.Add(new SqlParameter("@Record", SqlDbType.Int));
- spList[0].Value = tableName;
- spList[1].Value = fields;
- spList[2].Value = orderField;
- spList[3].Value = sqlWhere;
- spList[4].Value = pageSize;
- spList[5].Value = pageIndex;
- spList[6].Direction = ParameterDirection.Output;
- spList[7].Direction = ParameterDirection.Output;
- List<T> list = ServiceBase<T>.excuteSql(new T(), type, "exec_page", CommandType.StoredProcedure, spList.ToArray());
- //获取总页数
- try
- {
- totalPage = Convert.ToInt32(spList[6].Value);
- }
- catch
- {
- totalPage = 0;
- }
- try
- {
- totalRecord = Convert.ToInt32(spList[7].Value);
- }
- catch
- {
- totalRecord = 0;
- }
- return list;
- }
- }
- }
|