2.sql 分页
   注意:首先要得到pageSize,pageIndex
   select * from 
      (
           select top pageSize * from
               (
                  select top pageSize*pageIndex from Test order by id desc
               )
            as A order by id desc
      )
   Test
 select top  20*4的方式在SQL中取前80条记录,你会失败,因为TOP子句有一个限制,就是TOP num中的num不能是一个计算值或表达式而必须是一个既定的常量-------------------------------------------------------------------------------
  declare @count int 
 set @count =2*10
   select * from 
      (
           select top 2* from
               (
                  select top @count from Test order by id desc
               )
            as A order by id desc
      )
   Test
 
 
https://files.cnblogs.com/tinachang021/aspnentpager.rar
2.AspnetPage控件使用分页
   1。在sql语句中没有进行分页的情况
        注意使用这个控件的时候,一定要个  this.AspNetPager1.RecordCount 赋值哦,不然这个控件将不显示。
         RecordCount 是总记录数,(数据库里检索出来的所有记录数)。。。  
         StartIndex=this.AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1)
         EnedIndex=this.AspNetPager1.PageSize
          详情看源码:
               

 Code
Code
 1 private DataTable  LoadLocation()
    private DataTable  LoadLocation()
 2
 
     {
{
 3 int totalCount = 0;
        int totalCount = 0;
 4 DataSet ds = new DataSet();
        DataSet ds = new DataSet();
 5 int pageIndex = Util.ConvertTo<int>(Request.QueryString["page"], 0);
        int pageIndex = Util.ConvertTo<int>(Request.QueryString["page"], 0);
 6 if (pageIndex > 0)
        if (pageIndex > 0)
 7
 
         {
{
 8 AspNetPager1.CurrentPageIndex = pageIndex;
            AspNetPager1.CurrentPageIndex = pageIndex;
 9 }
        }
10 ds = BLLMemberLocation.GetMemberLocation(this.AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1), this.AspNetPager1.PageSize, out totalCount);
        ds = BLLMemberLocation.GetMemberLocation(this.AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1), this.AspNetPager1.PageSize, out totalCount);
11 this.AspNetPager1.RecordCount = totalCount;
        this.AspNetPager1.RecordCount = totalCount;
12
13 if (totalCount <= this.AspNetPager1.PageSize)
        if (totalCount <= this.AspNetPager1.PageSize)
14
 
         {
{
15 this.AspNetPager1.Visible = false;
            this.AspNetPager1.Visible = false;
16 }
        }
17 else
        else
18
 
         {
{
19
20 this.AspNetPager1.Visible = true;
            this.AspNetPager1.Visible = true;
21 }
        }
22
23 if (ds != null)
        if (ds != null)
24
 
         {
{
25
26 this.repMyInfo.DataSource = ds;
            this.repMyInfo.DataSource = ds;
27 this.repMyInfo.DataBind();
            this.repMyInfo.DataBind();
28 }
        }
29 return null;
        return null;
30 
      
31 }
    }
32
33 public static DataSet GetMemberLocation(int pageindex, int pagesize, out int totalCount)
 public static DataSet GetMemberLocation(int pageindex, int pagesize, out int totalCount)
34
 
     {
{
35 DataSet ds = new DataSet();
        DataSet ds = new DataSet();
36 using (SqlConnection myConnection = new SqlConnection(connString))
        using (SqlConnection myConnection = new SqlConnection(connString))
37
 
         {
{
38 SqlCommand sqlcmd = new SqlCommand("app_GetMemLocation", myConnection);
            SqlCommand sqlcmd = new SqlCommand("app_GetMemLocation", myConnection);
39 sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.CommandType = CommandType.StoredProcedure;
40 sqlcmd.Parameters.Add("@MemberID", SqlDbType.UniqueIdentifier);
            sqlcmd.Parameters.Add("@MemberID", SqlDbType.UniqueIdentifier);
41 sqlcmd.Parameters["@MemberID"].Value = userID;
            sqlcmd.Parameters["@MemberID"].Value = userID;
42 ds = Util.GetPagedRecords(myConnection, sqlcmd, pageindex, pagesize, out totalCount);
            ds = Util.GetPagedRecords(myConnection, sqlcmd, pageindex, pagesize, out totalCount);
43 myConnection.Close();
            myConnection.Close();
44 }
        }
45 
       
46 return ds;
        return ds;
47 }
    }
48 public static DataSet GetPagedRecords(SqlConnection myConnection, SqlCommand sqlcmd, int pageIndex, int pageSize, out int totalRecords)
 public static DataSet GetPagedRecords(SqlConnection myConnection, SqlCommand sqlcmd, int pageIndex, int pageSize, out int totalRecords)
49
 
     {
{
50 int StartRecord = pageIndex;
        int StartRecord = pageIndex;
51 int MaxRecords = pageSize;
        int MaxRecords = pageSize;
52
53 if (pageIndex < 0)
        if (pageIndex < 0)
54 throw new ArgumentException("PageIndex cannot be negative");
            throw new ArgumentException("PageIndex cannot be negative");
55 if (pageSize < 1)
        if (pageSize < 1)
56 throw new ArgumentException("PageSize must be positive");
            throw new ArgumentException("PageSize must be positive");
57
58 long lBound = (long)pageIndex * pageSize;
        long lBound = (long)pageIndex * pageSize;
59 long uBound = lBound + pageSize - 1;
        long uBound = lBound + pageSize - 1;
60
61 if (uBound > System.Int32.MaxValue)
        if (uBound > System.Int32.MaxValue)
62
 
         {
{
63 throw new ArgumentException("PageIndex too big");
            throw new ArgumentException("PageIndex too big");
64 }
        }
65 myConnection.Open();
        myConnection.Open();
66 DataSet ds = new DataSet();
        DataSet ds = new DataSet();
67 DataTable dt = new DataTable();
        DataTable dt = new DataTable();
68 using (SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd))
        using (SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd))
69
 
         {
{
70 adapter.Fill(dt);
            adapter.Fill(dt);
71 totalRecords = dt.Rows.Count;
            totalRecords = dt.Rows.Count;
72 dt.Clear();
            dt.Clear();
73 adapter.Fill(ds, StartRecord, MaxRecords, "tbName");
            adapter.Fill(ds, StartRecord, MaxRecords, "tbName");
74 }
       }
75
76 myConnection.Close();
        myConnection.Close();
77 return ds;
        return ds;
78 }
    } 
这里还要注意哦。。。adapter.Fill(ds.startrecord,maxrecord,"tbName")//它也是取的是当前页的数据
    例如:startRecord=0,maxrecord=10
   那它取的是0-10这10条数据
                 10-20
                  20-30
                 .。。。。 
 
同时还要注意:AspNetPager控件它有url参数的哦:
         http://localhost:4849/AppointmentMaker/UserProfile/MemberLocation.aspx?page=2
           每次点击分页控件它将会把参数显示到url上:即page就是当前页
 
 
   2.在sql语句中进行分页的情况