最大的网站源码资源下载站,
结合一个存储过程,将分页做成最简单,请看以下源码
此分页类所操作的存储过程#region 此分页类所操作的存储过程
/**//*********************************************************
*
* 功能强大,配合以下这个存储过程
*
* *******************************************************/
/**//*
-- pager 1,10,0,0, 'employeeid>2 and employeeid<5 ' , 'employees','*','lastname',0
create procedure pager
@pageindex int,--索引页 1
@pagesize int,--每页数量2
@recordcount int out,--总行数3
@pagecount int out,--总页数4
@wherecondition nvarchar(1000),--查询条件5
@tablename nvarchar(500),--查询表名6
@selectstr nvarchar(500) = '*',--查询的列7
@order nvarchar(500),--排序的列8
@ordertype bit = 0, -- 设置排序类型, 非 0 值则降序 9
@groupby nvarchar(100) = ''
as
declare @strsql nvarchar(2000) -- 主语句
declare @strtmp nvarchar(1000) -- 临时变量
declare @strorder nvarchar(1000) -- 排序类型
if @ordertype != 0
begin
set @strtmp = '<(select min'
set @strorder = ' order by ' + @order +' desc'
end
else
begin
set @strtmp = '>(select max'
set @strorder = ' order by ' + @order +' asc'
end
set @strsql = 'select top ' + str(@pagesize) + ' ' + @selectstr + ' from '
+ @tablename + ' where ' + @order + '' + @strtmp + '(['
+ @order + ']) from (select top ' + str((@pageindex-1)*@pagesize) + ' ['
+ @order + '] from ' + @tablename + '' + @strorder + ') as tbltmp)'
+ @groupby + @strorder
if @wherecondition != ''
set @strsql = 'select top ' + str(@pagesize) + ' ' + @selectstr + ' from '
+ @tablename + ' where ' + @order + '' + @strtmp + '(['
+ @order + ']) from (select top ' + str((@pageindex-1)*@pagesize) + ' ['
+ @order + '] from ' + @tablename + ' where (' + @wherecondition + ') '
+ @strorder + ') as tbltmp) and (' + @wherecondition + ') ' + @groupby + @strorder
if @pageindex = 1
begin
set @strtmp = ''
if @wherecondition != ''
set @strtmp = ' where (' + @wherecondition + ')'
set @strsql = 'select top ' + str(@pagesize) + ' ' + @selectstr + ' from '
+ @tablename + '' + @strtmp + ' ' + @groupby + @strorder
end
exec (@strsql)
--print @strsql
if @wherecondition <>''
begin
set @strtmp = 'select -1 from ' + @tablename + ' where ' + (@wherecondition)
end
else
begin
set @strtmp = 'select -1 from ' + @tablename
end
exec sp_executesql @strtmp
set @recordcount = @@rowcount
-- 获取总页数
-- "ceiling"函数:取得不小于某数的最小整数
set @pagecount = ceiling(@recordcount * 1.0 / @pagesize)
go
*****************************************************************************/
/**//****************************************************************************
*
* 用法
*
* ***************************************************************************/
/**//*
dim ts as string = request.form.item("txtdate")
if (ts = "" or ts is nothing) then
ts = request.querystring("txtdate")
end if
dim ts2 as string = request.form.item("txtdate2")
if (ts2 = "" or ts2 is nothing) then
ts2 = request.querystring("txtdate2")
end if
dim ps as string = request.form.item("pageindex")
if (ps = "" or ps is nothing) then
ps = request.querystring("pageindex")
end if
dim t as integer = 2
dim p as integer = 1
if ts is nothing then
ts = ""
end if
if ps is nothing then
ps = ""
end if
if not (ps = "") then
p = integer.parse(ps)
end if
dim pager as pager = new pager
pager.pageindex = p
pager.pagesize = 20
pager.pagemode = pagemode.str
pager.wherecondition = "thedate between convert(datetime,'" + ts + "') and convert(datetime,'" + ts2 + "')"
'pager.wherecondition = " convert(char(10),thedate,120)= '" + ts + "'"
pager.tablename = "loadcountlog"
pager.selectstr = "*"
pager.order = "id"
pager.ordertype = false
dim dt as system.data.datatable = pager.getdatas(p)
mydatagrid.datasource = dt
mydatagrid.databind()
dim gourl as string = "webform1.aspx?txtdate=" + ts + "&txtdate2=" + ts2
me.label3.text = "共:" + pager.pagecount.tostring + "页," + pager.recordcount.tostring() + "条 <strong>" + pager.outpager(pager, gourl, false) + "</strong>"
*/
#endregion
using system;
using system.data;
using system.data.sqlclient;
using system.configuration;
using system.collections;
using system.text;
namespace solucky
{
/**//// <summary>
/// 分页模式
/// </summary>
public enum pagemode
{
/**//// <summary>
/// 数字分页
/// </summary>
num =0,
/**//// <summary>
/// 字符分页
/// </summary>
str =1
}
/**//// <summary>
/// 分页类,能过存储过程进行分页,功能相当强大。
/// </summary>
public class pager
{
private int pageindex = 0;
private int recordcount = 0;
private int pagesize = 20;
private int pagecount = 0;
private int rowcount = 0;
private string tablename = "";
private string wherecondition = "1=1";
private string selectstr = "*";
private string order = "";
private string procedure ="pager";
private bool ordertype = true;
private pagemode pagemode =pagemode.num;
private string sqlconnectionstring = configurationsettings.appsettings["database"];
private string databaseowner = "dbo";
数据连接#region 数据连接
/**//// <summary>
/// 数据连接字符串
/// </summary>
private string sqlconnectionstring
{
get
{
return this.sqlconnectionstring;
}
set
{
this.sqlconnectionstring=value;
}
}
/**//// <summary>
///获取连接实例
/// </summary>
/// <returns></returns>
private sqlconnection getsqlconnectionstring()
{
try
{
return new sqlconnection(sqlconnectionstring);
}
catch
{
throw new exception("sql connection string is invalid.");
}
}
/**//// <summary>
/// 数据对象所有者
/// </summary>
private string databaseowner
{
get
{
return this.databaseowner;
}
set{
this.databaseowner=value;
}
}
#endregion
public pager()
{
//
// todo: 在此处添加构造函数逻辑
//
//enum.parse(tyo
}
public pager(string connstr )
{
if (connstr!=null)
this.sqlconnectionstring=connstr;
}
#region
/**//// <summary>
/// 所要操作的存储过程名称,已有默认的分页存储过程
/// </summary>
public string procedure
{
get{
return this.procedure ;
}
set {
if (value==null || value.length <=0)
{
this.procedure="pager";
}
else
{
this.procedure=value;
}
}
}
/**//// <summary>
/// 当前所要显示的页面数
/// </summary>
public int pageindex
{
get
{
return this.pageindex;
}
set
{
this.pageindex = value;
}
}
/**//// <summary>
/// 总的页面数
/// </summary>
public int pagecount
{
get
{
return this.pagecount;
}
set
{
this.pagecount = value;
}
}
/**//// <summary>
/// 总行数
/// </summary>
public int recordcount
{
get
{
return this.recordcount;
}
set
{
this.recordcount = value;
}
}
/**//// <summary>
/// 每页条数
/// </summary>
public int pagesize
{
get
{
return this.pagesize;
}
set
{
this.pagesize = value;
}
}
/**//// <summary>
/// 表名称
/// </summary>
public string tablename
{
get
{
return tablename;
}
set
{
this.tablename = value;
}
}
/**//// <summary>
/// 条件查询
/// </summary>
public string wherecondition
{
get
{
return wherecondition;
}
set
{
wherecondition = value;
}
}
/**//// <summary>
/// 查询目标(搜索目标),比如:addtime as 时间,id as 编号
/// </summary>
public string selectstr
{
get
{
return selectstr;
}
set
{
selectstr = value;
}
}
/**//// <summary>
/// 排序的列
/// </summary>
public string order
{
get
{
return order;
}
set
{
order = value;
}
}
/**//// <summary>
/// 排序类型 true:asc false:desc
/// </summary>
public bool ordertype
{
get
{
return ordertype;
}
set
{
ordertype = value;
}
}
/**//// <summary>
/// 分页模式
/// </summary>
public pagemode pagemode
{
get
{
return this.pagemode;
}
set
{
this.pagemode = value;
}
}
/**//// <summary>
/// 得到当前返回的数量
/// </summary>
public int rowcount
{
get
{
return this.rowcount;
}
}
private string groupby;
public string groupby
{
get
{
return this.groupby;
}
set
{
this.groupby = value;
}
}
#endregion
/**//// <summary>
/// 分页查寻结果
/// </summary>
public datatable getdatas(int pageindex)
{
this.pageindex = pageindex;
pager pager = this;
//pager.pageindex = pageindex;
datatable returntb = pagination(ref pager).tables[0];
this.rowcount = returntb.rows.count;
return returntb;
}
/**//// <summary>
/// 分页操作存储过程函数
/// </summary>
/// <param name="pager"></param>
/// <returns></returns>
private dataset pagination(ref pager pager)
{
using ( sqlconnection myconnection = getsqlconnectionstring() )
{
sqldataadapter mycommand = new sqldataadapter(pager.databaseowner + "."+pager.procedure, myconnection);
mycommand.selectcommand.commandtype = commandtype.storedprocedure;
sqlparameter parameterpageindex = new sqlparameter("@pageindex", sqldbtype.int);
parameterpageindex.value = pager.pageindex;
mycommand.selectcommand.parameters.add(parameterpageindex);
sqlparameter parameterpagesize = new sqlparameter("@pagesize", sqldbtype.int);
parameterpagesize.value = pager.pagesize;
mycommand.selectcommand.parameters.add(parameterpagesize);
sqlparameter parameterrecordcount = new sqlparameter("@recordcount", sqldbtype.int);
parameterrecordcount.value = 0;
parameterrecordcount.direction = parameterdirection.inputoutput;
mycommand.selectcommand.parameters.add(parameterrecordcount);
sqlparameter parameterpagecount = new sqlparameter("@pagecount", sqldbtype.int);
parameterpagecount.value = 0;
parameterpagecount.direction = parameterdirection.inputoutput;
mycommand.selectcommand.parameters.add(parameterpagecount);
sqlparameter parameterwherecondition = new sqlparameter("@wherecondition", sqldbtype.nvarchar,500);
parameterwherecondition.value = pager.wherecondition;
mycommand.selectcommand.parameters.add(parameterwherecondition);
sqlparameter parametertablename = new sqlparameter("@tablename", sqldbtype.nvarchar,500);
parametertablename.value = pager.tablename;
mycommand.selectcommand.parameters.add(parametertablename);
sqlparameter parameterorder = new sqlparameter("@order", sqldbtype.nvarchar,500);
parameterorder.value = pager.order;
mycommand.selectcommand.parameters.add(parameterorder);
sqlparameter parameterselectstr = new sqlparameter("@selectstr", sqldbtype.nvarchar,500);
parameterselectstr.value = pager.selectstr;
mycommand.selectcommand.parameters.add(parameterselectstr);
sqlparameter parametergroupby = new sqlparameter("@groupby", sqldbtype.nvarchar, 100);
parametergroupby.value = pager.groupby;
mycommand.selectcommand.parameters.add(parametergroupby);
sqlparameter parameterordertype = new sqlparameter("@ordertype", sqldbtype.bit);
parameterordertype.value = pager.ordertype==false?0:1;
mycommand.selectcommand.parameters.add(parameterordertype);
dataset returnds = new dataset();
//sqldataadapter sqlda = mycommand.crnew sqldataadapter(mycommand);
mycommand.fill(returnds);
pager.pagecount = (int)parameterpagecount.value;
pager.recordcount = (int)parameterrecordcount.value;
return returnds;
}
}
生成分页#region 生成分页
/**//// <summary>
/// 生成分页格式
/// </summary>
/// <param name="pager"></param>
/// <param name="url"></param>
/// <param name="isbr"></param>
/// <returns></returns>
public string outpager(pager pager,string url,bool isbr)
{
stringbuilder returnourwml;
if(isbr)
{
returnourwml= new stringbuilder("["+ pager.pagecount.tostring() + "页," + pager.recordcount.tostring() +"条]<br/>");
}
else
{
returnourwml = new stringbuilder();
}
if (pager.pagemode == pagemode.num)
{
//分页每行显示的数量
int pagerscount = 10;
int pagers = 0;
int startint = 1;
int endint = pager.pagecount;
int i = 1;
string endstr = "";
if (pager.pagecount>pagerscount)
{
//double k = ;
pagers = pager.pageindex / pagerscount;
if (pagers == 0)
{
pagers = 1;
}
else if((pager.pageindex % pagerscount)!=0)
{
pagers +=1;
}
endint = pagers * pagerscount;
if (pager.pageindex <= endint)
{
startint = endint +1 - pagerscount;
if (startint <1)
{
startint = 1;
}
}
//显示数量不足时pagerscount
if (endint>=pager.pagecount)
{
endint = pager.pagecount;
}
else
{
//if (pager.pageindex)
endstr = " <a href=/"";
endstr += url + "&pageindex=" + (endint + 1).tostring() + "/" title='第"+ (endint + 1).tostring()+"页'>";
endstr += ">>";
endstr += "</a> ";
}
if (pagers > 1)
{
returnourwml.append(" <a href=/"");
returnourwml.append(url + "&pageindex=" + (startint - 1).tostring() + "/" title='第"+ (startint - 1).tostring()+"页'>");
returnourwml.append("<<");
returnourwml.append("</a> ");
}
}
for (i = startint; i<=endint;i++)
{
if (i!=pager.pageindex)
{
returnourwml.append(" <a href=/"");
returnourwml.append(url + "&pageindex=" + i.tostring() + "/" title='第"+ i.tostring()+"页'>");
returnourwml.append("["+i.tostring() + "]");
returnourwml.append("</a> ");
}
else
{
returnourwml.append("<u>"+ i.tostring() + "</u>");
}
}
returnourwml.append(endstr);
return returnourwml.append("<br/>").tostring();
}
else
{
if ( pager.pageindex > 1)
{
returnourwml.append(" <a href=/"");
returnourwml.append(url + "&pageindex=" + (pager.pageindex -1).tostring() + "/">");
returnourwml.append("上一页");
returnourwml.append("</a> ");
}
if (pager.pageindex < pager.pagecount)
{
returnourwml.append(pager.pageindex.tostring());
returnourwml.append(" <a href=/"");
returnourwml.append(url + "&pageindex=" + (pager.pageindex +1).tostring() + "/">");
returnourwml.append("下一页");
returnourwml.append("</a> ");
}
return returnourwml.append("<br/>").tostring();
}
}
#endregion
}
}
新闻热点
疑难解答
图片精选