存储过程中处理分页数据
电脑版发表于:2023/4/25 15:33
前言
有个比较特殊的需求,从存储过程中处理分页数据。需要从存储过程中返回两个结果集(分页逻辑在存储过程中处理)
如下图
提供一个方法返回两个结果集,用键值对接收。
/// <summary> /// 查询数据,返回默认列表对象 /// </summary> /// <param name="cmdText">查询语句</param> /// <param name="pageIndex">第几页</param> /// <param name="pageSize">每页条数</param> /// <param name="param">查询参数</param> /// <param name="cmdType"></param> /// <returns></returns> public List<List<Dictionary<string, object>>> ExecuteDataTableResultByProc(string cmdText, Dictionary<string, object> param = null, CommandType cmdType = CommandType.StoredProcedure) { var result = new List<List<Dictionary<string, object>>>(); if (string.IsNullOrEmpty(cmdText)) return result; IDbConnection conn = ConnectionFactory.CreateConnection(_dbType, _strConn); using (conn) { if (conn.State == ConnectionState.Closed) conn.Open(); try { DynamicParameters dynamicParameters = new DynamicParameters(); foreach (var item in param.Keys) { dynamicParameters.Add("@" + item, param[item]); } var query = conn.QueryMultiple(cmdText, dynamicParameters, null, ConnectionTimeout, cmdType); while (!query.IsConsumed) { var tempResult = query.Read().ToList().Select(x => ((IDictionary<string, object>)x).ToDictionary(ks => ks.Key, vs => vs.Value)).ToList(); result.Add(tempResult); } } catch (Exception) { throw; } } return result; }