分页存储过程(一)使用sql2005的新函数构造分页
分页存储过程大致有下列几种
1、 利用Not in 和select top
2、 利用id大于多少和select top
3、 利用sql中的游标
4、临时表
可以参看网上的以下链接
在2005中我们的选择就多了,可以利用新语法CTE(公用表表达式),关于CTE的介绍大家可以参看博客园中一位仁兄的系列教程
或者干脆上微软的官网
查看具体内容。
除此之外还可以利用在2005中新增的一些函数,分别是:row_number(),rank,dense_rank,ntile,这些新函数是您可以有效的分析数据以及向查询饿结果行提供排序值。您可能发现这些新函数有用的典型方案包括:将连续整数分配给结果行,以便进行表示、分页、计分和绘制直方图。
详细介绍参见下列链接
我这里主要使用的就是row_number()结合新语法CTE,先贴上我的存储过程。设计,开发,测试存储过程和相关的C#代码就花费我两天的时间,不过后面的相似界面就很快了,一上午就可以搞两个分页显示的页面,就算是复杂的查询,一上午也可以搞定。
狼蚁网站SEO优化的存储过程没有将总页数和总条目数返回,如果你有兴趣,可以自己加上,可以参看 中的下列部分
Declare @sql nvarchar(4000);
Declare @totalRecord int;
--计算总记录数
if (@SqlWhere ='''' or @SqlWhere='' or @sqlWhere is NULL)
set @sql = 'select @totalRecord = count(*) from ' + @TableName
else
set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
--计算总页数
select @TotalPage=@totalRecord --CEILING((@totalRecord+0.0)/@PageSize)
存储过程SQL如下,支持不定列,不定条件,多表联合,排序任意
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--Declare @sql nvarchar(4000);
--Declare @totalRecord int;
----计算总记录数
--if (@SqlWhere ='''' or @SqlWhere='' or @sqlWhere is NULL)
--set @sql = 'select @totalRecord = count(*) from ' + @TableName
--else
--set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
--EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
--
----计算总页数
--
--select @TotalPage=@totalRecord --CEILING((@totalRecord+0.0)/@PageSize)
-- =============================================
-- Author: shiwenbin
-- MSN: jorden008@hotmail.com
-- Email: jorden008@163.com
-- Create date: 2009-10-20
-- Description: 分页存储过程,根据传递的参数返回分页的结果
-- Parameters:
-- =============================================
ALTER PROCEDURE [dbo].[Proc_GetDataPaged]
-- Add the parameters for the stored procedure here
@StrSelect varchar(max)=null, --欲显示的列(多列用逗号分开),例如:id,name
@StrFrom varchar(max)= null, --表名称,或者是表连接字符串,多表连接例如:student as s inner join dwinfo as dw on s.dwbh=dw.bh
@StrWhere varchar(max)=null, --查询条件,''代表没有条件,单条件或者多条件,多条件例如:name='啊' and id=10
@StrOrder varchar(max) =null, --排序列(多个排序列用逗号分开),例如:id desc,name as
--@PageCount int output, --总页数
@ItemCount bigint output, --总记录数
@PageSize int =50, --每页显示条数
@BeginIndex int=1,--记录开始数
@DoCount bit =0 --是否统计总数,为0不统计,为1统计
-- @PageIndex int =1 --当前页
--@ClassCode char(10) =null, --单位编号(班级编号)
AS
BEGIN
SET NOCOUNT ON;
Declare @sql nvarchar(4000);
Declare @totalRecord int;
--计算总记录数
if (@StrWhere ='''' or @StrWhere='' or @StrWhere is NULL)
set @sql = 'select @totalRecord = count(*) from ' + @StrFrom
else
set @sql = 'select @totalRecord = count(*) from ' + @StrFrom + ' where ' + @StrWhere
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@ItemCount OUTPUT--计算总记录数
declare @SqlQuery varchar(max)
-- if(@PageIndex=1)
if(@BeginIndex=1 or @BeginIndex=0 or @BeginIndex <0)
begin
if(@StrWhere is null)--if(@StrWhere='')
set @SqlQuery='select top '+convert(varchar,@PageSize)
+ ' row_number() over(order by '+@StrOrder+' ) as RowNumber,'+@StrSelect+
' from '+@StrFrom;
else
--set @sql='select top @PageSize * from @TableName order by id desc';
--select top @PageSize * from @TableName order by id desc;
set @SqlQuery='select top '+convert(varchar,@PageSize)
+ ' row_number() over(order by '+@StrOrder+' ) as RowNumber,'+@StrSelect+' from '+@StrFrom+' where '+@StrWhere;
--exec (@SqlQuery)
-- @SqlQuery
end
else
begin
if(@StrWhere is null)--if(@StrWhere='')
begin
set @SqlQuery='with cte as (
select row_number() over(order by '+@StrOrder+' ) as RowNumber,'+@StrSelect+' from '+@StrFrom+'
)
select * from cte where RowNumber between '+
--convert(varchar,((@PageIndex-1)*@PageSize)+1)+' and '+
--
-- convert(varchar,@PageIndex*@PageSize)
convert(varchar,@BeginIndex)+' and '+
convert(varchar,@BeginIndex+@PageSize)
--print @SqlQuery
end
else
begin
set @SqlQuery='with cte as (
select row_number() over(order by '+@StrOrder+' ) as RowNumber,'+@StrSelect+' from '+@StrFrom+' where '+@StrWhere+'
)
select * from cte where RowNumber between '+
--convert(varchar,((@PageIndex-1)*@PageSize)+1)+' and '+
--
-- convert(varchar,@PageIndex*@PageSize)
convert(varchar,@BeginIndex)+' and '+
convert(varchar,@BeginIndex+@PageSize)
--print @SqlQuery
end
end
--set @SqlQuery=@SqlQuery+';select @ItemCount =count(*) from '+@TableName
--set @PageCount=@ItemCount/@PageSize
--print '共'+@PageConut+'页'+@ItemCount+'条'
--print @ItemCount
print @SqlQuery
exec (@SqlQuery)
END
c#相关代码的数据库访问使用的是微软的企业库 V4.1
Enterprise Library 4.1 下载地址:
示例代码,前台页面,前台为用户控件
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StudentDetailsTable.ascx.cs" Inherits="Kimbanx.UCS.ForeignStudentAdmin.UserControl.UserControl.StudentDetailsTable" %>
<link href="../css/core.css" rel="stylesheet" type="text/css" />
<table class="StudentPagingTablePanel">
<tr>
<td> 单位:<asp:Label ID="lblClassName" runat="server" Text="Label"></asp:Label></td>
<td>级别:<asp:Label ID="lblClassLevel" runat="server" Text="Label"></asp:Label>级节点</td>
</tr>
<tr>
<td>该单位共有<asp:Label ID="lblStudentType" runat="server" Text="Label"></asp:Label>学员
<asp:Label ID="lblStudentCount" runat="server" Text="Label"></asp:Label>人</td>
<td>每页显示<asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlPageSize_SelectedIndexChanged">
</asp:DropDownList>人 共<asp:Label ID="lblPageCount" runat="server" Text="Label"></asp:Label>页
现为第<asp:Label ID="lblPageIndex" runat="server" Text="Label"></asp:Label>页
<asp:LinkButton ID="firstPage" runat="server" CommandArgument="first"
oncommand="LinkButton_Command">首页</asp:LinkButton>
<asp:LinkButton ID="nextPage" runat="server" CommandArgument="next"
oncommand="LinkButton_Command">下一页</asp:LinkButton>
<asp:LinkButton ID="prevPage" runat="server" CommandArgument="prev"
oncommand="LinkButton_Command">上一页</asp:LinkButton>
<asp:LinkButton ID="lastPage" runat="server" CommandArgument="last"
oncommand="LinkButton_Command">末页</asp:LinkButton>
</td>
</tr>
</table>
<br />
<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False"
EmptyDataText="没有符合条件的数据">
<Columns>
<asp:TemplateField HeaderText="照片">
<ItemTemplate>
<asp:Image ID="Image1" CssClass="studentImage" ImageUrl =<%# GetStudentImageUrl(Eval("zpadress")) %> runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名(中英简)">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("xmjz") %>'></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text='<%# Eval("xmjy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="jx" HeaderText="军衔" />
<asp:BoundField DataField="zw" HeaderText="职务" />
<asp:BoundField DataField="gj" HeaderText="国家" />
<asp:BoundField DataField="sjyqk" HeaderText="文化程度" />
<asp:BoundField DataField="zj" HeaderText="宗教" />
<asp:TemplateField HeaderText="出生/入伍">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# SetBirthDate(Eval("csrq")) %>'></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Text='<%# SetEnrollDate(Eval("rwrq")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="xzz" HeaderText="房间/楼号" />
<asp:TemplateField HeaderText="电话/小号">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("dhd") %>'></asp:Label>
<br />
<asp:Label ID="Label6" runat="server" Text='<%# Eval("dhx") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="fcjp" HeaderText="返程机票" />
<asp:BoundField DataField="xh" HeaderText="学号" />
</Columns>
</asp:GridView>
示例代码,后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Kimbanx.UCS.ForeignStudentAdmin.Model;
using Kimbanx.UCS.ForeignStudentAdmin.Common;
namespace Kimbanx.UCS.ForeignStudentAdmin.UserControl.UserControl
{
public partial class StudentDetailsTable : System.Web.UI.UserControl
{
private Database _db = DatabaseFactory.CreateDatabase();
private DbCommand _command;
private DbConnection _connection;
private DataSet _ds;
private string _classCode;
private string _classFullName;
private string _studentType;
private string _studentCount;
private string _queryStringWhere;
private DataTable _studentTable;
protected string SetBirthDate(object obj)
{
string result = string.Empty;
string temp = obj.ToString();
result = DateTime.Parse(temp).ToShortDateString();
return result;
}
protected string SetEnrollDate(object obj)
{
string result = string.Empty;
string temp = obj.ToString();
result = DateTime.Parse(temp).ToShortDateString();
return result;
}
protected void Filldata_dllPageSize()
{
for (int i = 1; i < 100; i++)
{
ddlPageSize.Items.Add(i.ToString());
}
ddlPageSize.SelectedIndex = 14;
}
protected void InitSession()
{
//Session["PageSize"] = 0;
Session["PageIndex"] = 1;
Session["PageCount"] = int.Parse(_studentCount) / 15 + 1;
}
/// <summary>
/// 获取QueryString传递参数
/// </summary>
protected void GetQueryStringPara()
{
_classCode = Request.QueryString["dwbh"];
_classFullName =HttpUtility.UrlDecode( Request.QueryString["dwmc"]);
_studentCount = Request.QueryString["studentCount"];
_studentType =HttpUtility.UrlDecode( Request.QueryString["studentType"]);
_queryStringWhere = Request.QueryString["where"];
}
protected void SetLabelText()
{
this.lblClassName.Text = _classFullName;
this.lblClassLevel.Text = GetClassInfo(_classCode).Level.ToString();
this.lblStudentCount.Text = _studentCount;
this.lblStudentType.Text = _studentType;
}
#region
///// <summary>
///// 获取学员数据
///// </summary>
///// <param name="strSelect">显示的字段</param>
///// <param name="strFrom">用到的</param>
/////<param name="strWhere">查询条件</param>
///// <param name="pageSize">每页显示条数</param>
///// <param name="pageIndex">当前页</param>
///// <returns></returns>
//protected DataTable GetStudentData(string strSelect,string strFrom,string strWhere,int pageSize,int pageIndex)
//{
// _command = _db.GetStoredProcCommand("StudentPaging");
// _db.AddInParameter(_command, "StrSelect", DbType.String, "zpadress,xmjz,xmjy,jx,zw,gj,sjyqk,zj,csrq,rwrq,xzz,dhd,dhx,fcjp,hzh,xh");
// _db.AddInParameter(_command, "StrFrom", DbType.String, "tx_xyzl");
// _db.AddInParameter(_command, "StrWhere", DbType.String, strWhere );
// _db.AddInParameter(_command, "StrOrder", DbType.String, "id");
// _db.AddInParameter(_command, "PageSize", DbType.Int32, pageSize );
// _db.AddInParameter(_command, "PageIndex", DbType.Int32,pageIndex );
// _studentTable = _db.ExecuteDataSet(_command).Tables[0];
// return _studentTable;
/