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]);

    }

  • 相关阅读:
    十七、S3C2440 音频解码芯片WM8976声卡驱动移植、madplay测试
    2.3 摄像头驱动_vivi驱动程序分析
    3.1 wifi网卡RT3070在S3C2440的移植和使用
    项目简介
    2.2 vivi虚拟视频驱动测试
    2.1 摄像头V4L2驱动框架分析
    LCD驱动程序编写
    LCD驱动程序
    1. Linux内核的配置与裁减:
    第5章 docker run、exec和attach使用和区别
  • 原文地址:https://www.cnblogs.com/jiangyunfeng/p/9054302.html
Copyright © 2011-2022 走看看