优秀的分页存储过程 -- asp#
来源:互联网 发布:英国西英格兰大学知乎 编辑:程序博客网 时间:2024/04/29 05:51
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(2000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(1000) -- 排序类型
if @OrderType != 0
begin
set @strTmp = "<(select min"
set @strOrder = " order by [" + @fldName +"] desc"
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @fldName +"] asc"
end
set @strSQL = "select top " + str(@PageSize) + " * 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) + " * from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @fldName + "] from [" + @tblName + "] where (" + @strWhere + ") "
+ @strOrder + ") as tblTmp) and (" + @strWhere + ") " + @strOrder
if @PageIndex = 1
begin
set @strTmp = ""
if @strWhere != ''
set @strTmp = " where (" + @strWhere + ")"
set @strSQL = "select top " + str(@PageSize) + " * from ["
+ @tblName + "]" + @strTmp + " " + @strOrder
end
if @IsCount != 0
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
exec (@strSQL)
GO
要注意看,修改后的存储过程在使用@strWhere时,都在其前后加上了(),这样,就防止嵌套的()出现错误
下面的代码是引用该存储过程的一个范例
SqlConnection MyConnection=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
DataSet MyDataSet=new DataSet();
string strKeyword=Keyword.Text.Trim().Replace("/'","/'/'");
string strSalesId=Sales.SelectedItem.Value;
int RecordCount=CalcRecordCount();
RecordNumber.Text=RecordCount.ToString();
LblRecordNumber.Text=RecordCount.ToString();
string strExpress="Id<>0";
if (strKeyword!="")
strExpress=strExpress+" and (companyenname like '%"+strKeyword+"%' orcompanychname like '%"+strKeyword+"%' or Companyshortname like'%"+strKeyword+"%' or web like '%"+strKeyword+"%' or mainproduct like'%"+strKeyword+"%' or phone like '%"+strKeyword+"%' or memo like'%"+strKeyword+"%' or address like '%"+strKeyword+"%' or linkmanphonelike '%"+strKeyword+"%')";
if (strSalesId!="")
strExpress=strExpress+" and salesid="+strSalesId;
SqlCommand MyCommand=new SqlCommand();
MyCommand.Connection=MyConnection;
MyCommand.CommandText="GetRecordFromPage";
MyCommand.CommandType=CommandType.StoredProcedure;
MyCommand.Parameters.Add("@tblName","customerview");
MyCommand.Parameters.Add("@fldName","id");
MyCommand.Parameters.Add("@strWhere",strExpress);
MyCommand.Parameters.Add("@PageSize",Int32.Parse(CustomerList.PageSize.ToString()));
MyCommand.Parameters.Add("@PageIndex",Int32.Parse(ViewState["PageIndex"].ToString())+1);
SqlDataReader MyReader;
MyConnection.Open();
MyReader=MyCommand.ExecuteReader();
CustomerList.VirtualItemCount=RecordCount;
CustomerList.DataSource=MyReader;
CustomerList.DataKeyField="id";
CustomerList.DataBind();
MyReader.Close();
MyConnection.Close();
在这里,要注意的是存储过程使用的PAGEINDEX变量是从1开始
就算条件再复杂,也能搞定
CREATE procedure page
(@pagesize int,
@pageindex int,
@docount bit)
as
set nocount on
if(@docount=1)
select count(UserID) from Users
else
begin
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
create table #pageindex(id int identity(1,1) not null,nid int)
set rowcount @PageUpperBound
insert into #pageindex(nid)
select UserID from Users order by UserID desc
select O.UserName,O.Uedate,UserID
from Users O,#pageindex p
where O.UserID=p.nid and p.id>@PageLowerBound and p.id<=@PageUpperBound order by p.id
end
GO
- 优秀的分页存储过程 -- asp#
- asp与存储过程的分页程序
- 优秀的百万级数据分页存储过程代码
- asp.net 分页控件与执行分页的存储过程
- asp通过存储过程分页
- Asp+Sqlserver2005 分页存储过程
- Asp.net 分页存储过程
- asp 存储过程(分页)
- asp存储过程分页源码
- Asp.Net分页存储过程
- ASP.NET存储过程分页
- ASP.NET存储过程分页
- Asp.Net分页存储过程
- Asp.Net分页存储过程
- asp.net 分页存储过程
- 分页的存储过程
- 分页的存储过程
- 分页的存储过程
- 数据库优化 - 日期
- [转]MapReduce:超大机群上的简单数据处理
- 十一有感
- 动态语言与闭包
- 每一粒砂都有它的价值
- 优秀的分页存储过程 -- asp#
- 界面遮罩(锁屏效果)
- 短信
- uclinux 多任务(各家之言)
- 做什么职业不后悔?
- MVC
- hibernate集合映射inverse和cascade详解
- 区别Ruby的require,load,和include
- 使用CFileDialog选择多个文件