发布使用SQL实现分页的类
本人看过,有不少人发帖问如何加快检索有几十万条记录的表显示在页面上。
看过不少人把所有记录也就是含有几十万条记录的结果集直接Fill到DataSet里面。
然后使用DataGrid的分页功能来显示几十条记录。
需知道,几十万条记录的结果集从MSSQL服务返回到Asp.Net服务就需要花费比较长的时间,Asp.Net把这几十万条记录再填充到DataSet里面要不知要花多少时间,然后DataGrid检索绑定DataTable要需要花多少时间。根据一般为提高服务速度的算法,短任务优先执行的原则。可以想象请求返回几十万条记录的线程在MSSQL和Asp.Net服务里面的优先级别比较低,更增加响应时间。更要命的是,Asp.Net的程序很多PostBack,导致提交一次,就要把上面的事情在忙一次。这样服务器很容易崩溃。
有不少人提出的解决方案是使用存储过程实现分页,这是一个好办法。但是不是所有人都会写存储过程。下面本人提供一个简单的类配置SQL语句实现分页的功能。
public class SqlPagination
{
#region Private 属性
string _Table="";
/// <summary>
/// 当前页
/// </summary>
int _CurrentPage=1;
/// <summary>
/// 每页返回多少条记录
/// </summary>
int _PageSize=1;
/// <summary>
/// 其它排序字段
/// </summary>
string _OrderField="";
/// <summary>
/// 排序关键字段,必填。
/// </summary>
string _KeyField="";
/// <summary>
/// 字段
/// </summary>
string _Fields="";
#endregion
#region Public 属性
public string Table
{
set{ _Table=value;}
get {
if(_Table=="")
throw new Exception ("Table属性不能为空");
else
return _Table;
}
}
/// <summary>
/// 当前页
/// </summary>
public int CurrentPage
{
get {return _CurrentPage;}
set
{
_CurrentPage=value;
if(_CurrentPage==0)
_CurrentPage=1;
}
}
/// <summary>
/// 每页返回多少条记录
/// </summary>
public int PageSize
{
get {return _PageSize;}
set
{
_PageSize=value;
if(_PageSize==0)
_PageSize=1;
}
}
/// <summary>
/// 其它排序字段
/// </summary>
public string OrderField
{
set {_OrderField=value;}
get { return _OrderField;}
}
/// <summary>
/// 排序关键字段,必填。
/// </summary>
public string KeyField
{
get
{
if(_KeyField.Trim()=="")
throw new Exception("缺少对KeyField属性进行赋值");
else
return _KeyField;
}
set {_KeyField=value;}
}
/// <summary>
/// 字段
/// </summary>
public string Fields
{
set {_Fields=value;}
get
{
if(_Fields.Trim()=="")
return "*";
else
return _Fields;
}
}
#endregion
#region Public字段
/// <summary>
/// 排序关键字段的数据类型,必填。
/// </summary>
public MSSqlDbType KeyFieldType;
public bool Desc=false;
public string Condition="";
#endregion
#region Public 函数
public SqlPagination(string Table,string KeyField,MSSqlDbType KeyFieldType)
{
this.Table=Table; this.KeyField=KeyField;
this.KeyFieldType=KeyFieldType;
}
/// <summary>
/// 返回分页的查询语句
/// </summary>
/// <returns></returns>
public string Select()
{
if (this.KeyFieldType==null)
throw new Exception ("KeyFieldType属性不能为空。");
string temp="";
string AndCondition="",WhereCondition="";
if (this.Condition.Trim()!="")
{
WhereCondition=" WHERE "+this.Condition;
AndCondition=" AND "+this.Condition;
}
if(!this.Desc) //升序 使用 > 不用desc 找 max
{
temp+="select top "+ this.PageSize.ToString()+ " " + this.Fields + " from "+ this.Table;
temp+=" where "+ this.KeyField +">(select isnull(max("+this.KeyField+"),"+ this.KeyFieldType.Min + ") from";
temp+=" (select top "+(this.PageSize*(this.CurrentPage-1)).ToString()+" " + this.KeyField+" from "+this.Table+WhereCondition;
temp+=" order by "+this.KeyField +") a)"+ AndCondition +" order by "+this.KeyField;
}
else
{
temp+="select top "+ this.PageSize.ToString()+ " " + this.Fields + " from "+ this.Table;
temp+=" where "+ this.KeyField +"<(select isnull(min("+this.KeyField+"),"+ this.KeyFieldType.Max + ") from";
temp+=" (select top "+(this.PageSize*(this.CurrentPage-1)).ToString()+" " + this.KeyField+" from "+this.Table+WhereCondition;
temp+=" order by "+this.KeyField +" desc) a)"+ AndCondition +" order by "+this.KeyField +" desc";
}
return temp;
}
#endregion
}