asp.net利用存储过程分页代码
-最通用的分页存储过程
-- 获取指定页的数据
CREATE PROCEDURE Pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@doCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from ['+ @tblName +'] where '+ @strWhere
else
set @strSQL = 'select count(*) as Total from ['+ @tblName +']'
end
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都
--是@doCount为0的情况
else
begin
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ['+ @fldName +'] desc'
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ['+ @fldName +'] asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName +'] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName +'] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + '])
from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from ['+ @tblName +']' + @strOrder + ') as tblTmp)'+ @strOrder if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName +'] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + ']
from ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder end end exec ( @strSQL)
GO
asp.net利用存储过程分页sqlserver代码
C#代码
using System.Data ;
using System.Data.SqlClient ;
using Microsoft.ApplicationBlocks.Data ;
using System.Web ;
using System.Web.UI ;
namespace RssLayer.PageHelper
{
/**//// <summary>
/// 分页类PagerHelper 的摘要说明。
/// </summary>
public class PagerHelper
{
private string connectionString;
public PagerHelper(string tblname,string sortname,bool docount,string connectionString)
{
this.tblName = tblname;
this.fldName = sortname ;
this.connectionString = connectionString ;
this.docount = docount;
} public PagerHelper(string tblname,bool docount,
string strGetFields, string fldName,int pagesize,
int pageindex,bool ordertype,string strwhere,string connectionString
)
{
this.tblName = tblname ;
this.docount = docount ;
this.strGetFields = strGetFields ;
this.fldName = fldName;
this.pagesize = pagesize ;
this.pageindex = pageindex;
this.ordertype = ordertype ;
this.strwhere = strwhere ;
this.connectionString = connectionString ; } /**//// <summary>
/// 得到记录集的构造函数
/// </summary>
/// <param name="tblname"></param>
/// <param name="strwhere"></param>
/// <param name="connectionString"></param>
public PagerHelper(string tblname,string strwhere,string connectionString)
{
this.tblName = tblname;
this.strwhere = strwhere ;
this.docount = true;
this.connectionString = connectionString ;
} private string tblName;
public string TblName
{
get{return tblName;}
set{tblName =value;}
} private string strGetFields="*";
public string StrGetFields
{
get{return strGetFields ;}
set{strGetFields =value;}
} private string fldName=string.Empty;
public string FldName
{
get{return fldName ;}
set{fldName =value;}
} private int pagesize =;
public int PageSize
{
get{return pagesize ;}
set{pagesize =value;}
} private int pageindex =;
public int PageIndex
{
get{return pageindex ;}
set{pageindex =value;}
} private bool docount=false;
public bool DoCount
{
get{return docount ;}
set{docount =value;}
} private bool ordertype=false;
public bool OrderType
{
get{return ordertype ;}
set{ordertype =value;}
} private string strwhere=string.Empty ;
public string StrWhere
{
get{return strwhere ;}
set{strwhere =value;}
} public IDataReader GetDataReader()
{ if(this.docount)
{
throw new ArgumentException("要返回记录集,DoCount属性一定为false");
}
// System.Web.HttpContext.Current.Response.Write(pageindex); return SqlHelper.ExecuteReader(connectionString,CommandType.StoredProcedure,"Pagination",
new SqlParameter("@tblName",this.tblName),
new SqlParameter("@strGetFields",this.strGetFields),
new SqlParameter("@fldName",this.fldName),
new SqlParameter("@PageSize",this.pagesize),
new SqlParameter("@PageIndex",this.pageindex),
new SqlParameter("@doCount",this.docount),
new SqlParameter("@OrderType",this.ordertype),
new SqlParameter("@strWhere",this.strwhere)
);
}
public DataSet GetDataSet()
{
if(this.docount)
{
throw new ArgumentException("要返回记录集,DoCount属性一定为false");
} return SqlHelper.ExecuteDataset(connectionString,CommandType.StoredProcedure,"Pagination",
new SqlParameter("@tblName",this.tblName),
new SqlParameter("@strGetFields",this.strGetFields),
new SqlParameter("@fldName",this.fldName),
new SqlParameter("@PageSize",this.pagesize),
new SqlParameter("@PageIndex",this.pageindex),
new SqlParameter("@doCount",this.docount),
new SqlParameter("@OrderType",this.ordertype),
new SqlParameter("@strWhere",this.strwhere)
);
}
public int GetCount()
{
if(!this.docount)
{
throw new ArgumentException("要返回总数统计,DoCount属性一定为true");
} return (int)SqlHelper.ExecuteScalar(connectionString,CommandType.StoredProcedure,"Pagination",
new SqlParameter("@tblName",this.tblName),
new SqlParameter("@strGetFields",this.strGetFields),
new SqlParameter("@fldName",this.fldName),
new SqlParameter("@PageSize",this.pagesize),
new SqlParameter("@PageIndex",this.pageindex),
new SqlParameter("@doCount",this.docount),
new SqlParameter("@OrderType",this.ordertype),
new SqlParameter("@strWhere",this.strwhere)
);
} }
}
asp.net利用存储过程分页后台代码
asp.net利用存储过程分页代码的更多相关文章
- ASP.NET MVC 简单分页代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- (转)ASP与sql存储过程
本文转载自:http://www.cnblogs.com/Spring/archive/2006/10/18/532817.aspx ASP与存储过程(Stored Procedures)的文章不少, ...
- ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...
- ASP.NET MVC + EF 利用存储过程读取大数据
ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK 看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, ...
- ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender
(原文) 昨天在ASP.NET MVC利用PagedList分页(一)的 最后一节提到,一个好的用户体验绝对不可能是点击下一页后刷新页面,所以今天来说说利用Ajax+PagedList实现无刷新(个人 ...
- C#高效分页代码(不用存储过程)
首先创建一张表(要求ID自动编号): create table redheadedfile ( id ,), filenames ), senduser ), primary key(id) ) 然后 ...
- Oracle利用存储过程性 实现分页
分页的简单配置 在上一次已经说过了 这边说说怎么在存储过程中实现分页 首先建立存储过程 參考 http://www.cnblogs.com/gisdream/archive/2011/11/16/22 ...
- ASP.NET MVC利用PagedList分页(一)
前几天看见博客园上有人写ASP.NET MVC的分页思想,这让我不禁想起了PagedList.PagedList是NuGet上提供的一个分页的类库,能对任何IEnumerable<T>进行 ...
- ASP.NET MVC 数据分页
作为一个菜鸟级的程序猿,总结一下学到的两种数据分页. 1.真分页 真分页就是需要时从数据库里读出需要多的数据,利用存储过程可实现.网上的分页SQL特别多,数据库自带的一些方法也可方便的帮助分页,但是我 ...
随机推荐
- expdp ORA-39070:Unable to open the log file
Oracle中,当执行expdp或impdp的时候,有时候会出现错误: [oracle@bi-dw ~]$ expdp dp_user/dp_password@dw directory=expdp_d ...
- 基于Centos搭建 Discuz 论坛
系统要求:CentOS 6.8 64 位操作系统 搭建 Discuz 论坛 准备 LAMP 环境 LAMP 是 Linux.Apache.MySQL 和 PHP 的缩写,是 Discuz 论坛系统依赖 ...
- 检查死锁与Locked ownable synchronizers(转载)
通过jstack可快速检查线程的死锁信息,用法如下: # 获取JVM ID(JAVA 进程ID),通过参数lv可以获取更详细的JAVA方法调用信息 jps -lv # 得到JVM ID后,执行jsta ...
- openssl命令行工具简介 - 指令x509
原文链接: http://blog.csdn.net/allwtg/article/details/4982507 openssl命令行工具简介 - 指令x509 用法: open ...
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
原文地址:http://www.entityframeworktutorial.net/code-first/database-initialization-in-code-first.aspx EF ...
- sublime text 3 安装卸载插件和取消启动检查更新
1.Perferences->Package Contro,输入install 调出 Install Package 选项并回车 2.安装相应的插件 2.点击首选项–设置(用户) 3.卸载插件 ...
- Django项目实战 - html中用户登录判断
实现逻辑: {% if request.user.is_authenticated %} 个人信息{% else %}登录{% endif %} 直接上代码 {% if request.user.is ...
- remmina如何上传文件到linux服务器
1.安装filezilla 2. remmina中选择列表中的主机,点击“外部工具 -> filezilla-xfp.sh” 3. 弹出filezilla,输入主机,端口,用户名,密码,连接, ...
- mysql 核心知识要点
整体知识介绍:mysql基本操作和使用,mysql优化(索引,分表等),mysql部署(读写分离,负载均衡等) 数据库基本介绍:数据库概念,常用数据库,web应用三大软件分工,PHP动态语言特点(处理 ...
- 批量 kill mysql 线程
时常有一些烂sql跑在数据库里,我们要进行kill,避免影响拖垮数据库. mysql> show processlist; +----+------+---------------------+ ...