当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net结合aspnetpager使用SQL2005的存储过程分页

ASP.NET
ASP.NET应用程序设计的10大技巧
asp.net开发与web标准的冲突问题
总结:ADO.NET中容易混淆的概念
ASP.NET数据库编程入门
ASP.Net网络数据库:连接到数据库
ASP.Net之C#中的异常处理
在C++中使用Lambda函数提高代码性能
从零开始学习jQuery (一) 开天辟地入门篇
.Net中的 XmlReader 与 XmlWriter 解析
ASP.NET安全问题--创建安全的Web应用程序
从零开始学习jQuery (二) 万能的选择器
ASP.NET开发必须养成的编程习惯
ASP.NET应用XML技术实现Web报表打印
ASP.NET实现静态的TreeView控件导航
数据结构与算法:C#语言描述 目录
ASP.NET创建XML Web服务全接触
使.NET命名空间符合标准
ASP.Net获得新浪天气预报几种方式总结
.Net技术开发中两个“属性”引起的歧异
.Net课堂:ASP.NET常用的优化性能方法

ASP.NET 中的 asp.net结合aspnetpager使用SQL2005的存储过程分页


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 100 ::
收藏到网摘: n/a

项目中用到了,同事阿春写了例子,并在实际项目中使用了,记录下。感谢春哥的无私奉献。 SQL2005的存储过程:
复制代码 代码如下:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[P_GetPagedReCord]
(@startIndex INT, -- 开始索引号
@endindex INT, -- 结束索引号
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 显示字段名
@OrderfldName varchar(255), -- 排序字段名
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(100) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @OrderType!=0
Begin
set @strOrder='Desc'
End
else
Begin
set @strOrder='Asc'
End
set @strSQL ='WITH orderList AS ( '+
'SELECT ROW_NUMBER() OVER (ORDER BY '+@OrderfldName+' '+@strOrder+')AS Row, '+@fldName+' '+
'from '+@tblName
if @strWhere!=''
set @strSQL = @strSQL+' where ' + @strWhere
set @strSQL=@strSQL+')'+
'SELECT '+@fldName+' '+
'FROM orderlist '+
'WHERE Row between '+str(@startIndex)+' and '+str(@endIndex)+''

if @IsReCount != 0
Begin
set @strSQL = ' select count(1) as Total from [' + @tblName + ']'
if @strWhere!=''
set @strSQL = @strSQL+' where ' + @strWhere
End
--print(@strSQL)
exec (@strSQL)

使用中的关键代码:
复制代码 代码如下:

//翻页
protected void anpager_PageChanged(object sender, EventArgs e)
{
bind();
}
string strWhere = " 1 = 1 ";
ETHaiNan.BLL.ET_Video bll = new ETHaiNan.BLL.ET_Video();
this.anpager.RecordCount = int.Parse(bll.GetRecordCount(strWhere).Tables[0].Rows[0][0].ToString());
this.anpager.PageSize = 10;
this.anpager.AlwaysShow = true;
DataSet ds = bll.GetList(anpager.StartRecordIndex, anpager.EndRecordIndex,strWhere, 0);
this.rpt.DataSource = ds;
ds.Dispose();

数据访问层:
复制代码 代码如下:

/// <summary>
/// 分页获取数据列表
/// </summary>
public DataSet GetList(int startIndex, int endindex, string strWhere, int IsReCount)
{
SqlParameter[] parameters = {
new SqlParameter("@startIndex", SqlDbType.Int),
new SqlParameter("@endindex", SqlDbType.Int),
new SqlParameter("@tblName", SqlDbType.VarChar, 255),
new SqlParameter("@fldName", SqlDbType.VarChar, 255),
new SqlParameter("@OrderfldName", SqlDbType.VarChar, 255),
new SqlParameter("@IsReCount", SqlDbType.Bit),
new SqlParameter("@OrderType", SqlDbType.Bit),
new SqlParameter("@strWhere", SqlDbType.VarChar,1000)
};
parameters[0].Value = startIndex;
parameters[1].Value = endindex;
parameters[2].Value = "ET_Video";
parameters[3].Value = "VideoID,Video_Name,Video_TypeID,Video_OrderCode,Video_Type,Video_AddDate";
parameters[4].Value = "VideoID";
parameters[5].Value = IsReCount;
parameters[6].Value = 1;
parameters[7].Value = strWhere;
return DbHelperSQL.RunProcedure("P_GetPagedReCord", parameters, "ds");
}