调用存储过程分页
1 go 2 create proc r_students 3 ( 4 @StudentName varchar(20),--学生姓名 模糊查询 5 @ClassId int, --班级ID 精确查询 6 @PageSize int , --页面大小 每页放几条记录 7 @PageIndex int, --页码 第几页 8 @RecordCount int output,--输出参数 记录总数 总条数 9 @PageCount int output --输出参数 总页数 10 ) 11 as 12 begin 13 declare @where varchar(max) 14 set @where=' where 1=1 ' 15 if(@StudentName!='') 16 begin 17 set @where=@where+' and StudentName like''%'+@StudentName+'%''' 18 end 19 if(@ClassId!=-1) 20 begin 21 set @where=@where+' and ClassId = '+str(@ClassId) 22 end 23 select @RecordCount=count(1) from TbStudent where StudentName like '%'+@StudentName+'%' and (@ClassId=-1 or ClassId=@ClassId) 24 set @PageCount=CEILING(@RecordCount*1.0/@pagesize) 25 declare @start int,@end int 26 set @start=(@PageIndex-1)*@PageSize+1 27 set @end=@PageIndex*@PageSize 28 declare @sql varchar(max) 29 set @sql='select * from 30 (select ROW_NUMBER() over(order by StudentName) Hid, a.ClassName,b.* from TbClass a join TbStudent b on a.Id=b.ClassId '+@where+' ) 31 temp where Hid>='+STR(@start)+' and Hid<='+str(@end)+'' 32 exec(@sql) 33 end 34 declare @eq int,@ec int 35 exec r_students '王',-1,5,1,@eq output,@ec output 36 select @eq,@ec
存储过程分页,通过api调用存储过程进行分页,操作过程在dal中实现,通过一个新类来存储数据库中调用过来的内容
调用后创建一个分页类来接收值,方便后续分页,之间的传值,
再使用一个新类的集合字段来接收这个集合传来的值
其中注意SQLparameter实例化要实例化为数组,同时给多个参数赋值,最后传入前台就可以分页了
1 public PageList show(int pagesize,string name="",int pageindex=1,int tid=-1) 2 { 3 SqlParameter[] sq = new SqlParameter[] 4 { 5 new SqlParameter{ ParameterName="@StudentName",DbType=DbType.String,Value=name}, 6 new SqlParameter{ ParameterName="@ClassId",DbType=DbType.Int32,Value=tid}, 7 new SqlParameter{ ParameterName="@PageSize",DbType=DbType.Int32,Value=pagesize}, 8 new SqlParameter{ ParameterName="@PageIndex",DbType=DbType.Int32,Value=pageindex}, 9 new SqlParameter{ ParameterName="@RecordCount",DbType=DbType.Int32,Direction=ParameterDirection.Output}, 10 new SqlParameter{ ParameterName="@PageCount",DbType=DbType.Int32,Direction=ParameterDirection.Output}, 11 }; 12 List<Students> list = db.Database.SqlQuery<Students>("r_students @StudentName,@ClassId,@PageSize,@PageIndex,@RecordCount output,@PageCount output",sq).ToList(); 13 PageList p = new PageList(); 14 p.Rcount = Convert.ToInt32(sq[4].Value); 15 p.Pcount= Convert.ToInt32(sq[5].Value); 16 p.Plist = list; 17 return p; 18 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探