SQL分页ROW_NUMBER() OVER/TOP MAX/TOP MIN存贮过程调用方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Linq;
namespace Pub.Class {
public class PagerProc {
private static int cacheSeconds = 1440;
public static IList<TResult> ToList<TResult>(int pageIndex,int pageSize,string tableName,string pk,string fieldList,string sortList, bool isDescSort,string where,bool isDist,out int totalRecords, bool useCache) where TResult : class,new() {
if (string.IsNullOrEmpty(fieldList)) fieldList = "*";
totalRecords = 0; IList<TResult> list = null;
string cacheNameKey = string.Empty; string cacheRecordsKey = string.Empty;
if (useCache) {
string cacheName = "{2}Cache_SelectPageList_{0}_{1}_{3}_{4}_{5}_{6}_{7}_{8}";
string cacheRecords = "{2}Cache_RecordsSelectPageList_{0}_{1}_{3}_{4}_{5}_{6}_{7}_{8}";
cacheNameKey = string.Format(cacheName, pageIndex, pageSize, tableName, pk, fieldList, sortList, isDescSort, where, isDist);
cacheRecordsKey = string.Format(cacheRecords, pageIndex, pageSize, tableName, pk, fieldList, sortList, isDescSort, where, isDist);
list = (IList<TResult>)Cache2.Get(cacheNameKey);
if (list != null) { totalRecords = (int)Cache2.Get(cacheRecordsKey); return list; }
}
DbParameter[] dbParams = {
Data.MakeInParam("@page", (DbType)SqlDbType.Int, 4, pageIndex),
Data.MakeInParam("@pageSize", (DbType)SqlDbType.Int, 4, pageSize),
Data.MakeInParam("@tblName", (DbType)SqlDbType.NVarChar, 2000, tableName),
Data.MakeInParam("@ID", (DbType)SqlDbType.NVarChar, 150, pk),
Data.MakeInParam("@fldName", (DbType)SqlDbType.NVarChar, 500, fieldList),
Data.MakeInParam("@fldSort", (DbType)SqlDbType.NVarChar, 200, sortList),
Data.MakeInParam("@Sort", (DbType)SqlDbType.Bit, 1, isDescSort),
Data.MakeInParam("@strCondition", (DbType)SqlDbType.NVarChar, 1000, where),
Data.MakeInParam("@Dist", (DbType)SqlDbType.Bit, 1, isDist),
Data.MakeOutParam("@Counts", (DbType)SqlDbType.Int, totalRecords),
};
list = Data.GetDbDataReader("getPagerTopMAX", dbParams).ToList<TResult>();
if (list == null) return null;
totalRecords = (int)dbParams[9].Value;
if (useCache) {
Cache2.Insert(cacheNameKey, list, cacheSeconds);
Cache2.Insert(cacheRecordsKey, totalRecords, cacheSeconds);
}
return list;
}
public static IList<TResult> ToList<TResult>(int pageIndex,int pageSize,string tableName,string pk,string fieldList,string where,out int totalRecords, bool useCache) where TResult : class,new() {
return ToList<TResult>(pageIndex, pageSize, tableName, pk, fieldList, pk, true, where, false, out totalRecords, useCache);
}
public static IList<TResult> ToList<TResult>(int pageIndex,int pageSize,string tableName,string pk,string fieldList,string where,out int totalRecords) where TResult : class,new() {
return ToList<TResult>(pageIndex, pageSize, tableName, pk, fieldList, where, out totalRecords, false);
}
public static IList<TResult> ToList<TResult>(int pageIndex,int pageSize,string tableName, string fieldList,string where,string groupBy,string orderBy,out int totalRecords, bool useCache) where TResult : class,new() {
if (string.IsNullOrEmpty(fieldList)) fieldList = "*";
totalRecords = 0; IList<TResult> list = null;
string cacheNameKey = string.Empty; string cacheRecordsKey = string.Empty;
if (useCache) {
string cacheName = "{2}Cache_SelectPageList_{0}_{1}_{3}_{4}_{5}_{6}";
string cacheRecords = "{2}Cache_RecordsSelectPageList_{0}_{1}_{3}_{4}_{5}_{6}";
cacheNameKey = string.Format(cacheName, pageIndex, pageSize, tableName, fieldList, where, groupBy, orderBy);
cacheRecordsKey = string.Format(cacheRecords, pageIndex, pageSize, tableName, fieldList, where, groupBy, orderBy);
list = (IList<TResult>)Cache2.Get(cacheNameKey);
if (list != null) { totalRecords = (int)Cache2.Get(cacheRecordsKey); return list; }
}
DbParameter[] dbParams = {
Data.MakeInParam("@PageIndex", (DbType)SqlDbType.Int, 4, pageIndex),
Data.MakeInParam("@PageSize", (DbType)SqlDbType.Int, 4, pageSize),
Data.MakeInParam("@Tables", (DbType)SqlDbType.NVarChar, 1000, tableName),
Data.MakeInParam("@Fields", (DbType)SqlDbType.NVarChar, 2000, fieldList),
Data.MakeInParam("@Where", (DbType)SqlDbType.NVarChar, 2000, where),
Data.MakeInParam("@GroupBy", (DbType)SqlDbType.NVarChar, 2000, groupBy),
Data.MakeInParam("@OrderBy", (DbType)SqlDbType.NVarChar, 1000, orderBy),
Data.MakeOutParam("@ReturnCount", (DbType)SqlDbType.Int, totalRecords),
};
list = Data.GetDbDataReader("getPagerROWOVER", dbParams).ToList<TResult>();
if (list == null) return null;
totalRecords = (int)dbParams[7].Value;
if (useCache) {
Cache2.Insert(cacheNameKey, list, cacheSeconds);
Cache2.Insert(cacheRecordsKey, totalRecords, cacheSeconds);
}
return list;
}
}
}
开源:
https://github.com/hcxiong 欢迎收藏:)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?