您的位置:首页 > 其它

不用存储实现的分页,效率和存储过程一样。

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: