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
详情看源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
private DataTable LoadLocation()
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
int totalCount = 0;
4
DataSet ds = new DataSet();
5
int pageIndex = Util.ConvertTo<int>(Request.QueryString["page"], 0);
6
if (pageIndex > 0)
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
AspNetPager1.CurrentPageIndex = pageIndex;
9
}
10
ds = BLLMemberLocation.GetMemberLocation(this.AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1), this.AspNetPager1.PageSize, out totalCount);
11
this.AspNetPager1.RecordCount = totalCount;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
if (totalCount <= this.AspNetPager1.PageSize)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
this.AspNetPager1.Visible = false;
16
}
17
else
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
this.AspNetPager1.Visible = true;
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
if (ds != null)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
this.repMyInfo.DataSource = ds;
27
this.repMyInfo.DataBind();
28
}
29
return null;
30
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
public static DataSet GetMemberLocation(int pageindex, int pagesize, out int totalCount)
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
35
DataSet ds = new DataSet();
36
using (SqlConnection myConnection = new SqlConnection(connString))
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
SqlCommand sqlcmd = new SqlCommand("app_GetMemLocation", myConnection);
39
sqlcmd.CommandType = CommandType.StoredProcedure;
40
sqlcmd.Parameters.Add("@MemberID", SqlDbType.UniqueIdentifier);
41
sqlcmd.Parameters["@MemberID"].Value = userID;
42
ds = Util.GetPagedRecords(myConnection, sqlcmd, pageindex, pagesize, out totalCount);
43
myConnection.Close();
44
}
45
46
return ds;
47
}
48
public static DataSet GetPagedRecords(SqlConnection myConnection, SqlCommand sqlcmd, int pageIndex, int pageSize, out int totalRecords)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
50
int StartRecord = pageIndex;
51
int MaxRecords = pageSize;
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
if (pageIndex < 0)
54
throw new ArgumentException("PageIndex cannot be negative");
55
if (pageSize < 1)
56
throw new ArgumentException("PageSize must be positive");
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
long lBound = (long)pageIndex * pageSize;
59
long uBound = lBound + pageSize - 1;
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
if (uBound > System.Int32.MaxValue)
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
throw new ArgumentException("PageIndex too big");
64
}
65
myConnection.Open();
66
DataSet ds = new DataSet();
67
DataTable dt = new DataTable();
68
using (SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd))
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
adapter.Fill(dt);
71
totalRecords = dt.Rows.Count;
72
dt.Clear();
73
adapter.Fill(ds, StartRecord, MaxRecords, "tbName");
74
}
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
myConnection.Close();
77
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语句中进行分页的情况