不用存储实现的分页,效率和存储过程一样。
2005-10-17 10:22
429 查看
/// <summary>
/// 获取分页操作SQL语句(对于排序的字段必须建立索引)
/// </summary>
/// <param name="tblName">操作表名</param>
/// <param name="fldName">操作索引字段名称</param>
/// <param name="PageIndex">当前页</param>
/// <param name="PageSize">每页显示记录数</param>
/// <param name="rtnFields">返回字段集合,中间用逗号格开。返回全部用“*”</param>
/// <param name="OrderType">排序方式,0升序,1为降序</param>
/// <param name="strWhere">检索的条件语句,不需要再加WHERE关键字</param>
/// <returns></returns>
public static string ConstructSplitSQL(string tblName,
string fldName,
int PageIndex,
int PageSize,
string rtnFields,
int OrderType,
string strWhere)
{
string strSQL = "";
string strOldWhere = "";
// 构造检索条件语句字符串
if( strWhere != "" )
{
strOldWhere = " AND " + strWhere + " ";
strWhere = " WHERE " + strWhere + " ";
// 去除不合法的字符,防止SQL注入式攻击
strWhere = strWhere.Replace("'", "''");
strWhere = strWhere.Replace("--", "");
strWhere = strWhere.Replace(";", "");
}
// 升序操作
if( OrderType == 0 )
{
if( PageIndex == 1 )
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += strWhere + "ORDER BY " + fldName + " ASC";
}
else
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += "WHERE (" + fldName + " > ( SELECT MAX(" + fldName + ") FROM (SELECT TOP " + ((PageIndex - 1)*PageSize) + " " + fldName + " FROM " + tblName + strWhere + " ORDER BY " + fldName + " ASC ) AS T )) ";
strSQL += strOldWhere + "ORDER BY " + fldName + " ASC";
}
}
// 降序操作
else if( OrderType == 1 )
{
if( PageIndex == 1 )
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += strWhere + "ORDER BY " + fldName + " DESC";
}
else
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += "WHERE (" + fldName + " < ( SELECT MIN(" + fldName + ") FROM (SELECT TOP " + ((PageIndex - 1)*PageSize) + " " + fldName + " FROM " + tblName + strWhere + " ORDER BY " + fldName + " DESC ) AS T )) ";
strSQL += strOldWhere + "ORDER BY " + fldName + " DESC";
}
}
else // 异常处理
{
throw new DataException("未指定任何排序类型。0升序,1为降序");
}
return strSQL;
}
调用代码:cs文件的,参考。。
DataProvider dp = null;
user.dataType = "SqlClient";
user.connectionString = "server=(local); uid=sa; pwd=1016; database=skyBoard";
dp = user.InstanceDataProvider();
string strCmd = "SELECT Count(ID) FROM [address]";
int totalRecord = 1198954;
int PageSize = 20;
int PageIndex = (Request.QueryString["page"] == null) ? 1 : int.Parse(Request.QueryString["page"]);
int pageNum = 0;
if(totalRecord % PageSize == 0)
{
pageNum = totalRecord/PageSize;
}
else
{
pageNum = (totalRecord/PageSize) + 1;
}
//strCmd = String.Format(Seaskyer.FSO.FObject.ReadFile(@"E:\\a.txt"), PageSize * (PageIndex - 1) + 1);
strCmd = Seaskyer.Strings.Function.ConstructSplitSQL("address", "ID", PageIndex, PageSize, "*", 1, "");
Response.Write(strCmd);
DataTable dt = dp.DataTableSQL(strCmd);
Response.Write("共有" + totalRecord + ", " + user.SplitPages("test.aspx?", PageIndex, pageNum, totalRecord ));
DataGrid1.DataSource = dt.DefaultView;
DataGrid1.DataBind();
dt.Clear();
dt.Dispose();
/// 获取分页操作SQL语句(对于排序的字段必须建立索引)
/// </summary>
/// <param name="tblName">操作表名</param>
/// <param name="fldName">操作索引字段名称</param>
/// <param name="PageIndex">当前页</param>
/// <param name="PageSize">每页显示记录数</param>
/// <param name="rtnFields">返回字段集合,中间用逗号格开。返回全部用“*”</param>
/// <param name="OrderType">排序方式,0升序,1为降序</param>
/// <param name="strWhere">检索的条件语句,不需要再加WHERE关键字</param>
/// <returns></returns>
public static string ConstructSplitSQL(string tblName,
string fldName,
int PageIndex,
int PageSize,
string rtnFields,
int OrderType,
string strWhere)
{
string strSQL = "";
string strOldWhere = "";
// 构造检索条件语句字符串
if( strWhere != "" )
{
strOldWhere = " AND " + strWhere + " ";
strWhere = " WHERE " + strWhere + " ";
// 去除不合法的字符,防止SQL注入式攻击
strWhere = strWhere.Replace("'", "''");
strWhere = strWhere.Replace("--", "");
strWhere = strWhere.Replace(";", "");
}
// 升序操作
if( OrderType == 0 )
{
if( PageIndex == 1 )
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += strWhere + "ORDER BY " + fldName + " ASC";
}
else
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += "WHERE (" + fldName + " > ( SELECT MAX(" + fldName + ") FROM (SELECT TOP " + ((PageIndex - 1)*PageSize) + " " + fldName + " FROM " + tblName + strWhere + " ORDER BY " + fldName + " ASC ) AS T )) ";
strSQL += strOldWhere + "ORDER BY " + fldName + " ASC";
}
}
// 降序操作
else if( OrderType == 1 )
{
if( PageIndex == 1 )
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += strWhere + "ORDER BY " + fldName + " DESC";
}
else
{
strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
strSQL += "WHERE (" + fldName + " < ( SELECT MIN(" + fldName + ") FROM (SELECT TOP " + ((PageIndex - 1)*PageSize) + " " + fldName + " FROM " + tblName + strWhere + " ORDER BY " + fldName + " DESC ) AS T )) ";
strSQL += strOldWhere + "ORDER BY " + fldName + " DESC";
}
}
else // 异常处理
{
throw new DataException("未指定任何排序类型。0升序,1为降序");
}
return strSQL;
}
调用代码:cs文件的,参考。。
DataProvider dp = null;
user.dataType = "SqlClient";
user.connectionString = "server=(local); uid=sa; pwd=1016; database=skyBoard";
dp = user.InstanceDataProvider();
string strCmd = "SELECT Count(ID) FROM [address]";
int totalRecord = 1198954;
int PageSize = 20;
int PageIndex = (Request.QueryString["page"] == null) ? 1 : int.Parse(Request.QueryString["page"]);
int pageNum = 0;
if(totalRecord % PageSize == 0)
{
pageNum = totalRecord/PageSize;
}
else
{
pageNum = (totalRecord/PageSize) + 1;
}
//strCmd = String.Format(Seaskyer.FSO.FObject.ReadFile(@"E:\\a.txt"), PageSize * (PageIndex - 1) + 1);
strCmd = Seaskyer.Strings.Function.ConstructSplitSQL("address", "ID", PageIndex, PageSize, "*", 1, "");
Response.Write(strCmd);
DataTable dt = dp.DataTableSQL(strCmd);
Response.Write("共有" + totalRecord + ", " + user.SplitPages("test.aspx?", PageIndex, pageNum, totalRecord ));
DataGrid1.DataSource = dt.DefaultView;
DataGrid1.DataBind();
dt.Clear();
dt.Dispose();
相关文章推荐
- 不用存储实现的分页,效率和存储过程一样
- 不用存储过程的分页实现
- 利用SQL未公开的存储过程实现分页
- 实现千万级数据分页的存储过程http://hi.baidu.com/lanxmail/blog/item/2820274632c8b0086a63e5a0.html
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- 实现小数据量和海量数据的通用分页显示存储过程
- 使用为公开的存储过程sp_cursoropen实现分页
- 通过游标实现分页存储过程
- 实现分页的存储过程
- 1 用存储过程实现分页,除了上一页,下一页,第一页,和末页外还要有go按钮,以及go到那里的文本框。另外还要在Lable显示“当前x页,一共y页”。注意验证控件的使用和 链接存储过程的内容。
- 在存储过程中实现分页.
- Sql分页存储过程以及实现
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页[转]
- 7.2.1 TOP n 实现的通用分页存储过程.sql
- 存储过程实现分页
- oracle实现存储过程分页查询(非包)
- 实现小数据量和海量数据的通用分页显示存储过程
- 存储过程实现分页