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特别多,数据库自带的一些方法也可方便的帮助分页,但是我 ...
随机推荐
- document.domain 实现跨域
该方式只能用于二级域名相同的情况下,比如 a.test.com 和 b.test.com 适用于该方式. 只需要给页面添加 document.domain = 'test.com' 表示二级域名都相同 ...
- mysqld_safe 无法启动的原因
[root@localhost ~]# 170408 09:53:13 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdom ...
- RabbitMQ ——“Hello World”
介绍 RabbitMQ是一个消息实体服务(broker):它接收及转发消息.你可以把它想象成一个邮局:当你把你想要寄送的邮件放进邮箱里时,你能够确信邮局的派送员最终会把你的这封邮局送到这信的收件者手中 ...
- [Aaronyang] 写给自己的WPF4.5 笔记17[Page实现页面导航]
1. 第一个Page页使用 新建PageDemo解决方案,默认wpf应用程序 右键项目新建页,然后指定App.xaml的默认启动窗口,为Page1.xaml,F5运行项目 文章内容已经迁移http:/ ...
- css中border-radius用法详解
border-radius:由浮点数字和单位标识符组成的长度值.border-top-left-radius --- 左上border-top-right-radius --- 右上border-bo ...
- latex学习(三)
本文记录一点杂事. 1.vim下有个实时pdf预览的插件:https://github.com/xuhdev/vim-latex-live-preview 2.实时预览pdf的pdf查看器是:evin ...
- Java知多少(84)图形界面之布局设计
在界面设计中,一个容器要放置许多组件,为了美观,为组件安排在容器中的位置,这就是布局设计.java.awt中定义了多种布局类,每种布局类对应一种布局的策略.常用的有以下布局类: FlowLayout, ...
- SmileyCount.java笑脸加法程序代写(QQ:928900200)
SmileyCount.java 1/4Java Programming 2014Course Code: EBU4201Mini ProjectTask 1 [30 marks]SmileyCoun ...
- Scala学习笔记——类型
1.Option类型 Option类型可以有两种类型,一种是Some(x),一种是None对象 比如Scala的Map的get方法发现了指定键,返回Some(x),没有发现,返回None对象 2.列表 ...
- Unity Remote远程调试
http://www.cnblogs.com/qinghuaideren/p/3623368.html http://blog.csdn.net/u012741077/article/details/ ...