• sql server 带输入输出参数的分页存储过程(效率最高)


    create procedure proc_page_withtopmax
    (
     @pageIndex int,--页索引
     @pageSize int,--每页显示数
     @pageCount int output,--总页数,输出参数 
     @totalCount int output--总条数
    )
    as
    begin
    set nocount on;
    
    declare @sql nvarchar(1000)
    set @sql='select top 10 *  from tb_testtable where (id> (select max(id) from (select top '+str((@pageIndex-1)*@pageSize)+' id from tb_testtable order by id) as temp)) order by id'
    execute(@sql)
    
    declare @sqlRecordCount nvarchar(1000) --得到总记录条数的语句
    set @sqlRecordCount=N'select @recordCount=count(*) from tb_testtable' 
    declare @recordCount int --保存总记录条数的变量
    exec sp_executesql @sqlRecordCount,N'@recordCount int output',@recordCount output 
    
    if( @recordCount % @pageSize = 0) --如果总记录条数可以被页大小整除 
    set @pageCount = @recordCount / @pageSize --总页数就等于总记录条数除以页大小 
    else --如果总记录条数不能被页大小整除 
    set @pageCount = @recordCount / @pageSize + 1 --总页数就等于总记录条数除以页大小加1 
    
    set @totalCount = @recordCount
    
    set nocount off;
    end
    
    --数据库中执行该存储过程
    declare @pageCount int, @totalCount int
    
    exec proc_page_withtopmax 2,95955,@pageCount output,@totalCount output
    
    select '总页数:'+str(@pageCount)
    select '总条数:'+str(@totalCount)

    C# 代码调用该带输入输出参数的分页存储过程

      public static DataSet GetRecordByPage( int pageSize, int pageIndex, out int pageCount, out int totalCount)
            {
                DataSet ds = new DataSet();
                try
                {
                    using (SqlConnection conn = new SqlConnection(@"server=;database=data_test;uid=; pwd=;"))
                    {
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = conn;
                        cmd.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.Int));
                        cmd.Parameters.Add(new SqlParameter("@pageIndex", SqlDbType.Int));
                        SqlParameter param = new SqlParameter("@totalCount", SqlDbType.Int);
                        param.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(param);
    
                        SqlParameter param1 = new SqlParameter("@pageCount", SqlDbType.Int);
                        param1.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(param1);
    
                        cmd.Parameters[0].Value = pageSize;
                        cmd.Parameters[1].Value = pageIndex;
                        cmd.Parameters[2].Value = 0;
                        cmd.Parameters[3].Value = 0;
    
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "proc_page_withtopmax";
                        cmd.CommandTimeout = 180;
    
                        SqlDataAdapter adapter = new SqlDataAdapter();
                        adapter.SelectCommand = cmd;
    
                        DataSet source = new DataSet();
                        adapter.Fill(ds);
                     
                        object o = cmd.Parameters["@totalCount"].Value;
                        totalCount = (o == null || o == DBNull.Value) ? 0 : System.Convert.ToInt32(o);
    
                        object b = cmd.Parameters["@pageCount"].Value;
                        pageCount = (b == null || o == DBNull.Value) ? 0 : System.Convert.ToInt32(b);
                    }
                }
                catch (SqlException e)
                {
                    throw e;
                }
                return ds;
            }
    
        }
  • 相关阅读:
    java实现第四届蓝桥杯黄金连分数
    freemarker 取值(插值)(转)
    js获取ModelAndView值的问题
    kaptcha验证码的使用(转)
    springboot自定义错误页面(转)
    SpringBoot实战之异常处理篇
    BootstrapValidator实现注册校验和登录错误提示效果(转)
    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
    springmvc 通过异常增强返回给客户端统一格式
    SpringBoot-@RequestParam
  • 原文地址:https://www.cnblogs.com/lihfeiblogs/p/4126614.html
Copyright © 2020-2023  润新知