存储过程:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--参数说明-------------------------------------------------------------
/**//*
@strTable --要显示的表或多个表的连接
@strField --要查询出的字段列表,*表示全部字段
@intTop --最多读取记录数
@pageSize --每页显示的记录个数
@pageIndex --要显示那一页的记录
@strWhere --查询条件,不需where
@strSortKey --用于排序的主键
@strSortField --用于排序,如:id desc (多个id desc,dt asc)
@strOrderBy --排序,0-顺序,1-倒序
@pageCount --查询结果分页后的总页数
@RecordCount --查询到的总记录数
@UsedTime --耗时测试时间差
*/
CREATE PROCEDURE [dbo].[ThePagerIndex]
@strTable varchar(50) = '[dbo].[ttable]',
@strField varchar(50) = '*',
@intTop int = 5000,
@pageSize int = 20,
@pageIndex int = 1,
@strWhere varchar(50) = '1=1',
@strSortKey varchar(50) = 'id',
@strSortField varchar(50) = 'id DESC',
@strOrderBy bit = 1,
@pageCount int OUTPUT,
@RecordCount int OUTPUT
--@UsedTime int OUTPUT
AS
SET NOCOUNT ON
Declare @sqlcount INT
Declare @timediff DATETIME
select @timediff=getdate()
Begin Tran
DECLARE @sql nvarchar(max),@where1 varchar(max),@where2 varchar(max)
IF @strWhere is null or rtrim(@strWhere)=''
BEGIN--没有查询条件
SET @where1=' WHERE '
SET @where2=' '
END
ELSE
BEGIN--有查询条件
SET @where1=' WHERE ('+@strWhere+') AND ' --本来有条件再加上此条件
SET @where2=' WHERE ('+@strWhere+') ' --原本没有条件而加上此条件
END
--SET @sql='SELECT @intResult=COUNT(*) FROM '+@strTable+@where2
IF @intTop<=0
BEGIN
SET @sql='SELECT @sqlcount=COUNT(*) FROM (select '+@strSortKey+' from '+ @strTable + @where2 +') As tmptab'
END
ELSE
BEGIN
SET @sql='SELECT @sqlcount=COUNT(*) FROM (select top '+ cast(@intTop as varchar(max)) +' '+@strSortKey+' from '+ @strTable + @where2 +') As tmptab'
END
--print @sql
EXEC sp_executesql @sql,N'@sqlcount int OUTPUT',@sqlcount OUTPUT --计算总记录数
SELECT @pageCount=CEILING((@sqlcount+0.0)/@pageSize) --计算总页数
SELECT @RecordCount = @sqlcount --设置总记录数
IF @pageIndex=1 --第一页
BEGIN
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(max))+' '+@strField+' FROM '+@strTable+
@where2+'ORDER BY '+ @strSortField
END
Else
BEGIN
IF @strOrderBy=0
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(max))+' '+@strField+ ' FROM '+@strTable+@where1+@strSortKey+'>(SELECT MAX('+@strSortKey+') '+ ' FROM (SELECT TOP '+CAST(@pageSize*(@pageIndex-1) AS varchar(max))+' '+
@strSortKey+' FROM '+@strTable+@where2+'ORDER BY '+@strSortField+') t) ORDER BY '+@strSortField
ELSE
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(max))+' '+@strField+' FROM '+@strTable+@where1+@strSortKey+'<(SELECT MIN('+@strSortKey+') '+ ' FROM (SELECT TOP '+CAST(@pageSize*(@pageIndex-1) AS varchar(max))+' '+
@strSortKey+' FROM '+@strTable+@where2+'ORDER BY '+@strSortField+') t) ORDER BY '+@strSortField+''
END
EXEC(@sql)
--print @sql
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit TRAN
--set @UsedTime = datediff(ms,@timediff,getdate())
--select datediff(ms,@timediff,getdate()) as 耗时
Return @sqlcount
End
实体对象类:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///Question_model 的摘要说明
/// </summary>
public class Question_model
{
public Question_model()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
int qid;
public int Qid
{
get { return qid; }
set { qid = value; }
}
string qtitle;
public string Qtitle
{
get { return qtitle; }
set { qtitle = value; }
}
}
数据访问层 Question_bll.cs类
public IList<Question_model> GetPage(int pageindex, int _pageSize, out int pageCount, out int RecordCount)
{
pageCount = 0;
RecordCount = 0;
IList<Question_model> list = new List<Question_model>();
using (SqlConnection conn = new SqlConnection(PubConstant.ConnectionString))
{
SqlCommand objcmd = new SqlCommand(".ThePagerIndex", conn);
objcmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] para ={
new SqlParameter("@strTable",SqlDbType.VarChar,-1),
new SqlParameter("@strField",SqlDbType.VarChar,-1),
new SqlParameter("@pageSize",SqlDbType.Int),
new SqlParameter("@pageIndex",SqlDbType.Int),
new SqlParameter("@strSortKey",SqlDbType.VarChar,-1),
new SqlParameter("@strSortField",SqlDbType.VarChar,-1),
new SqlParameter("@strOrderBy",SqlDbType.Bit),
new SqlParameter("@pageCount",SqlDbType.Int),
new SqlParameter("@RecordCount",SqlDbType.Int),
new SqlParameter("@inttop",SqlDbType.Int,-1)
};
para[0].Value = "question";
para[1].Value = "*";
para[2].Value = _pageSize;
para[3].Value = pageindex;
para[4].Value = "qid";
para[5].Value = "qtime desc";
para[6].Value = 1;
para[7].Value = pageCount;
para[7].Direction = ParameterDirection.Output;
para[8].Value = RecordCount;
para[8].Direction = ParameterDirection.Output;
para[9].Value = -1;
objcmd.Parameters.AddRange(para);
conn.Open();
using (SqlDataReader reader = objcmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
Question_model model = new Question_model();
model.Qid = Convert.ToInt32(reader["qid"]);
model.Qtitle = Convert.ToString(reader["qtitle"]);
list.Add(model);
}
}
RecordCount = Convert.ToInt32(objcmd.Parameters["@RecordCount"].Value);
pageCount = Convert.ToInt32(objcmd.Parameters["@pageCount"].Value);
conn.Close();
conn.Dispose();
}
return list;
}
aspx代码:
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
分页测试<br />
</HeaderTemplate>
<ItemTemplate>
<span style="width:100">编号:<%#Eval("qid")%> <%#Eval("qtitle")%></span><br />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFooterTemplate" runat="server" Text="无相关数据" Visible="<%#bool.Parse((Repeater1.Items.Count==0).ToString())%>"></asp:Label>
</FooterTemplate>
</asp:Repeater>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanged="PageChanged" FirstPageText="首页" LastPageText="尾页"
NextPageText="下一页" PrevPageText="上一页" ShowInputBox="Always" Font-Size="13px" ShowPageIndexBox="Never" PageSize="5">
</webdiyer:AspNetPager>
</div>
aspx.cs代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Page_Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Bind();
}
}
private readonly Question_bll bll = new Question_bll();
int currPage = 1;
int PageSize = 5;
public void Bind()
{
int pageCount;
int RecordCount;
Repeater1.DataSource = bll.GetPage(currPage, PageSize, out pageCount, out RecordCount);
Repeater1.DataBind();
this.AspNetPager1.RecordCount = RecordCount;
this.AspNetPager1.CurrentPageIndex = currPage;
this.AspNetPager1.PageSize = PageSize;
}
protected void PageChanged(object sender, EventArgs e)
{
int pageCount;
int RecordCount;
Repeater1.DataSource = bll.GetPage(this.AspNetPager1.CurrentPageIndex, PageSize, out pageCount, out RecordCount);
Repeater1.DataBind();
}
}
转载:http://hi.baidu.com/wwaityuan/blog/item/4ef3c10bb81d0e32b0351d21.html