要达到此目的,就需要先获取当前查询条件下能获取的总的数据条数,然后再告诉服务器我要去那条到那条,也就是开始行的索引 (startIndex)和结束行的索引(endIndex),在SQLSERVER数据库中无法动态设定行号(也就是数据行的索引)的取值范围,所以这 里就必须要使用存储过程或者自定义函数。而在ORACLE数据库中有一个伪列的概念,即ROWNUM,其实也就是查询结果集数据行的索引,所以在 ORACLE中只通过查询语句便可实现真分页。
在SQLSERVER 2005中提供了一个在2000中没有的函数 -- -- row_number(),这个函数可以获取数据结果集行的索引。
1)在SQLSERVER中处理分页,必须使用存储过程或自定义函数
--通过开始行和结束行的索引获取结果集的存储过程
create procedure sp_Paging_GetCounty
@startIndex int,
@endIndex int
as
with table_temp as (select row_number() over(order by o_id) as rowIndex,* from County)
select * from table_temp where rowIndex between @startIndex and @endIndex
go
--通过开始行和结束行的索引获取结果集的函数
create function ft_Paging_GetCounty(@startIndex int,@endIndex int)
returns table
as
return(with table_temp as (select row_number() over(order by o_id) as rowIndex,* from County)
select * from table_temp where rowIndex between @startIndex and @endIndex)
go
--获取所有符合查询条件的结果集的总行数
select count(*) from County
执行以下操作时
exec sp_Paging_GetCounty 1,12
select * from ft_Paging_GetCounty(1,5)
select count(*) from County
测试结果如下:
2)在ORACLE中处理分页则相对简单,只需要两个查询即可
--通过开始行和结束行的索引获取结果集
select rownum,* from County where rownum between 1 and 8
--获取所有符合查询条件的结果集的总行数
select count(*) from County
和第二节中的自定义假分页相似,这里只是获取分页显示数据集的方式有所区别,真分页是按需获取,假分页则是获取所有然后再进行筛选。实例源码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>ASP.NET分页之自定义存储过程分页(真分页)</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="o_id"
Width="400px">
<Columns>
<asp:BoundField DataField="rowIndex" HeaderText="行索引" SortExpression="rowIndex" ReadOnly="True" />
<asp:BoundField DataField="o_id" HeaderText="编号" SortExpression="o_id" ReadOnly="True" />
<asp:BoundField DataField="o_name" HeaderText="区县" SortExpression="o_name" />
<asp:BoundField DataField="c_id" HeaderText="市编号" SortExpression="c_id" />
</Columns>
</asp:GridView>
<br />
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
共<asp:Label ID="lblPageCount" runat="server" ForeColor="Red"></asp:Label>页, 当前第<asp:Label
ID="lblCurrPage" runat="server" ForeColor="Red"></asp:Label>页 共<asp:Label
ID="lblTotalCount" runat="server" ForeColor="Red"></asp:Label>条记录, 每页显示<asp:Label
ID="lblPageSize" runat="server" Text="12" ForeColor="Red"></asp:Label>条
</td>
<td>
<asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" OnClick="btnPrev_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" OnClick="btnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click">尾页</asp:LinkButton>
跳到<asp:DropDownList ID="ddlPager" runat="server" Width="50" OnSelectedIndexChanged="ddlPager_SelectedIndexChanged"
AutoPostBack="true" />
页
</td>
</tr>
</table>
</form>
</body>
</html>
示例后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
public partial class Default4 : System.Web.UI.Page
{
//页面加载时默认显示第一页
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindPaging("1");
}
}
//首页按钮被点击
protected void btnFirst_Click(object sender, EventArgs e)
{
BindPaging("1");
}
//上一页页按钮被点击
protected void btnPrev_Click(object sender, EventArgs e)
{
BindPaging("-1");
}
//下一页页按钮被点击
protected void btnNext_Click(object sender, EventArgs e)
{
BindPaging("+1");
}
//尾页按钮被点击
protected void btnLast_Click(object sender, EventArgs e)
{
BindPaging("0");
}
//下拉列表框选项发生改变
protected void ddlPager_SelectedIndexChanged(object sender, EventArgs e)
{
BindPaging(ddlPager.Text);
}
private void BindPaging(string page)
{
SqlDataSource sqlds = new SqlDataSource();
sqlds.ConnectionString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
sqlds.SelectCommand = "SELECT count(*) FROM [County]";
sqlds.SelectCommandType = SqlDataSourceCommandType.Text;
DataView dv = sqlds.Select(DataSourceSelectArguments.Empty) as DataView;
int totalCount = int.Parse(dv[0][0].ToString());//总条数
int pageSize = 12;//每页显示条数
int pageCount = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1);//总页数
int currPage; //当前页数
//设置当前页
switch (page)
{
//首页
case "1":
currPage = 1;
break;
//下一页
case "+1":
currPage = int.Parse(lblCurrPage.Text) + 1;
break;
//上一页
case "-1":
currPage = int.Parse(lblCurrPage.Text) - 1;
break;
//尾页
case "0":
currPage = pageCount;
break;
//下拉列表框直接传值时
default:
currPage = int.Parse(page);
break;
}
//加载DropDownList项
if (!IsPostBack)
{
for (int i = 1; i <= pageCount; i++)
{
ddlPager.Items.Add(i.ToString());
}
}
//设置分页信息的显示
lblCurrPage.Text = currPage.ToString();
lblPageCount.Text = pageCount.ToString();
lblPageSize.Text = pageSize.ToString();
lblTotalCount.Text = totalCount.ToString();
//设置翻页按钮的可用性
btnFirst.Enabled = btnPrev.Enabled = btnNext.Enabled = btnLast.Enabled = true;
if (currPage == 1)
{
btnFirst.Enabled = btnPrev.Enabled = false;
}
if (currPage == pageCount)
{
btnNext.Enabled = btnLast.Enabled = false;
}
ddlPager.Text = currPage.ToString();
//通过开始行和结束行的索引获取当前页要显示的数据集
sqlds.SelectCommand = "sp_Paging_GetCounty";
sqlds.SelectParameters.Add("startIndex",((currPage - 1) * pageSize + 1).ToString());
sqlds.SelectParameters.Add("endIndex",(currPage * pageSize).ToString());
sqlds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
dv = sqlds.Select(DataSourceSelectArguments.Empty) as DataView;
GridView1.DataSource = dv;
GridView1.DataBind();
}
}
运行效果如下: