zoukankan      html  css  js  c++  java
  • 单条件存储过程分页(SQL Server)&WS调用(只是其中一种 实现的方法还有很多)

    if(exists (select * from sys.objects where name='proc_Paging'))
     drop proc proc_Paging
    go
    create proc proc_Paging
    (
     @CountryCode nvarchar(20),--国家编号
     @PageIndex int =1,--当前要显示的页码
     @PageSize int=3,  --每页要显示的数据条数(页大小) 
     @TotalPages int out --总页数,一个输出参数
    )
    as
    begin
     
     --分页的SQL
     select top (@PageSize) * from
     (select row_number() over(order by ID) as rowIndex, * from city where CountryCode=@CountryCode) T
     where rowIndex>((@PageIndex-1)*@PageSize)
     --总数据量(既 一共有多少条数据)
     declare @totalNum int  --接收总数据量
     select @totalNum =COUNT(1) from city where CountryCode=@CountryCode
     --计算得出总页数
     set @TotalPages =ceiling(@totalNum*1.0/@PageSize)  --ceiling是向上取整
    end
    --=====================================================================================================================
    --存储过程的调用
    declare @num int
    exec proc_Paging 'CHN',1,5,@num out
    print @num
     
     
            /// <summary>
            /// 分页的存储过程调用
            /// </summary>
            /// <returns></returns>
            public List<AskBillModel> GetList(out int pageCount, string name = "", int pageIndex = 1, int pageSize = 0)
            {
                SqlParameter[] paras = new SqlParameter[] {
                    new SqlParameter(){ ParameterName="@Name",SqlDbType=SqlDbType.NVarChar,SqlValue=name},
                    new SqlParameter(){ ParameterName="@PageIndex",SqlDbType=SqlDbType.Int,SqlValue=pageIndex},
                    new SqlParameter(){ ParameterName="@PageSize",SqlDbType=SqlDbType.Int,SqlValue=pageSize},
                    //Direction是指定参数的输入输出类型,Output代表输出参数,输出参数不需要给value值
                    new SqlParameter(){ ParameterName="@PageCount",SqlDbType=SqlDbType.Int,Direction=ParameterDirection.Output}
     
                 var list = DBHelper.GetListByProc<AskBillModel>("Proc_Paged", paras);
                 //一定要在执行了存储过程之后 再给输出的参数赋值
                   pageCount = Convert.ToInt32(paras[3].Value);
                   return list
                };
     
            /// <summary>
            /// 提供分页的相关数据
            /// </summary>
            /// <returns></returns>
            [WebMethod] //这是WS调用
            public PageModel GetPage(string name = "", int pageIndex = 1, int pageSize = 5)
            {
                PageModel model = new PageModel();
                int count = 0;
                model.BillList = dal.GetList(out count, name, pageIndex, pageSize);
                model.PageCount = count;
                return model;
            }
  • 相关阅读:
    java多线程执行时主线程的等待
    数据库锁机制
    数据库事务学习
    EF查询百万级数据的性能测试--多表连接复杂查询
    EF查询百万级数据的性能测试--单表查询
    一文看懂-Docker容器化
    一文看懂-Kafka消息队列
    一文看懂-ElasticSearch全文搜索引擎
    Linux系统(ubuntu)部署Asp.Net Core网站
    Linux系统学习(一)一Linux介绍
  • 原文地址:https://www.cnblogs.com/alives/p/13072149.html
Copyright © 2011-2022 走看看