zoukankan      html  css  js  c++  java
  • SQLServer数据库分页查询

    出表A中的第31条到40条记录(表A以自动增长的ID做主键,注意ID可能是不连续的
    --事先删除了ID 为33的数据
     
    --第一种
    select top 10 ID from T where ID not in(select top 30 ID from T ORDER BY ID ASC)ORDER BY ID
     例:select top (@pageSize) * from table where id not in (select top ((@pageIndex-1)*@pageSize) id from table order by id ) order by id 
    --第二种
    SELECT * FROM (select top 10 * FROM( select top 40 * from T order by ID asc)TT order by TT.ID DESC)TTT order by TTT.ID asc
    --第三种
    select * from T where T.ID in( select top 10 ID FROM(select top 40 ID from T order by T.ID asc)TT order by TT.ID desc) order by ID
    --第四种
    select * from( select ROW_NUMBER() over(order by ID)TT FROM T)TTT WHERE TTT.TT between 30 and 40
     
     
    分页存储过程的写法:
    create Proc P_LoadPageData
      --参数
      @pageSize int,
      @pageInderx int,
      @total int out
    as
      --代码
      select top (@pageSize) * from table where id not in
       (
        select top ((@pageIndex-1)*@pageSize) id from table order by id
       )  order by id 
     
      select total = count('a') from table
      select total
     
    --测试存储过程 
      declare @total int
      exec P_LoadPageData 3,5,@total out
      print @total
      select @total
     
    //以下是在.net中调中存储过程
     

    public System.Collections.Generic.List<Model.HKSJ_Main> LoadPageData(int pageIndex, int pageSize, out int total)
    {
    DataSet ds = new DataSet();

    SqlParameter totalParameter = new SqlParameter("@total", SqlDbType.Int);
    totalParameter.Direction = ParameterDirection.Output;
    //DbHelperSQL.RunProcedure()


    //如果用了输出参数,那么就用SqlDataAdapter就可以了,用sqlDataReader时候拿不到输出参数的值。
    using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
    {
    //conn.Open();
    using (SqlDataAdapter adapter = new SqlDataAdapter("P_LoadPageData", conn))
    {
    adapter.SelectCommand.Parameters.Add(new SqlParameter("@pageIndex", pageIndex));
    adapter.SelectCommand.Parameters.Add(new SqlParameter("@pageSize", pageSize));

    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

    //输出参数的用法
    adapter.SelectCommand.Parameters.Add(totalParameter);

    adapter.Fill(ds);
    }
    }
    total = (int)totalParameter.Value;//拿到输出参数的值

    return this.DataTableToList(ds.Tables[0]);

    }

  • 相关阅读:
    12_springmvc拦截器
    11_springmvc之RESTful支持
    10_springmvc JSON数据交互
    09_springmvc图片上传
    09_springmvc异常处理
    08_springmvc数据回显和@ModelAttribute注解详解
    Eclipse-----解决调试源码不进入断点问题
    JavaScript-----截取字符串的常用方法
    排序(Sort)-----冒泡排序
    SpringMVC探究-----常用获取传递参数的方法
  • 原文地址:https://www.cnblogs.com/jiangyunfeng/p/9054302.html
Copyright © 2011-2022 走看看