DataGrid基于Access的快速分页法(3)
发表于:2007-06-30来源:作者:点击数:
标签:
降序的 SQL 语句 降序的SQL语句跟升序的大同小异,这里就不在罗嗦了J (1)@PageIndex = @FirstIndex SELECT TOP @PageSize @QueryFields FROM @TableName WHERE @Condition ORDER BY @PrimaryKey DESC (2)@FirstIndex @PageIndex = @MiddleIndex SELECT T
降序的
SQL语句
降序的SQL语句跟升序的大同小异,这里就不在罗嗦了J
(1)@PageIndex <= @FirstIndex
SELECT TOP @PageSize @QueryFields FROM @TableName WHERE @Condition ORDER BY @PrimaryKey DESC
(2)@FirstIndex < @PageIndex <= @MiddleIndex
SELECT TOP @PageSize @QueryFields
FROM @TableName
WHERE @PrimaryKey < (
SELECT
MIN(@PrimaryKey) FROM (
SELECT TOP @PageSize*@PageIndex @PrimaryKey
FROM @TableName
WHERE @Condition
ORDER BY @PrimaryKey DESC
) TableA
) WHERE @Condition
ORDER BY @PrimaryKey DESC
(3)@MiddleIndex < @PageIndex < @LastIndex
SELECT * FROM (
SELECT TOP @PageSize @QueryFields
FROM @TableName
WHERE @PrimaryKey > (
SELECT MAX(@PrimaryKey) FROM (
SELECT TOP (@RecordCount-@PageSize*(@PageIndex+1)) @PrimaryKey
FROM @TableName
WHERE @Condition
ORDER BY @PrimaryKey ASC
) TableA
) WHERE @Condition
ORDER BY @PrimaryKey ASC
) TableB ORDER BY @PrimaryKey DESC
(4)@PageIndex >= @LastIndex
SELECT * FROM ( SELECT TOP (@RecordCount-@PageSize*@LastIndex) @QueryFields
FROM @TableName WHERE @Condition ORDER BY @PrimaryKey ASC
) TableA ORDER BY @PrimaryKey DESC
如何动态产生上述的SQL语句?
看了上面的SQL语句之后,相信大家已经基本明白该分页法的原理了。下面,我们将要设计一个动态生成SQL语句的类FastPaging。该类有一个公有静态方法,它根据您给出的条件动态生成SQL语句,作为方法的返回值。
// 产生根据指定字段排序并分页查询的 SELECT 语句。
public static String Paging(
int pageSize, //每页要显示的记录的数目。
int pageIndex, //要显示的页的索引。
int recordCount, //数据表中的记录总数。
String tableName, //要查询的数据表。
String queryFields, //要查询的字段。
String primaryKey, //主键字段。
bool ascending, //是否为升序排列。
String condition //查询的筛选条件。
) {
StringBuilder sb = new StringBuilder();
int pageCount = GetPageCount(recordCount,pageSize); //分页的总数
int middleIndex = GetMidPageIndex(pageCount); //中间页的索引
int firstIndex = 0; //第一页的索引
int lastIndex = pageCount - 1; //最后一页的索引
if (pageIndex <= firstIndex) {
// 代码略
} else if (pageIndex > firstIndex && pageIndex <= middleIndex) {
sb.Append("SELECT TOP ").Append(pageSize).Append(" ")
.Append(queryFields).Append(" FROM ").Append(tableName)
.Append(" WHERE ").Append(primaryKey);
if (ascending)
sb.Append(" > (").Append(" SELECT MAX(");
else
sb.Append(" < (").Append(" SELECT MIN(");
sb.Append(primaryKey).Append(") FROM ( SELECT TOP ")
.Append(pageSize*pageIndex).Append(" ").Append(primaryKey)
.Append(" FROM ").Append(tableName);
if (condition != String.Empty)
sb.Append(" WHERE ").Append(condition);
sb.Append(" ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending)).Append(" ) TableA )");
if (condition != String.Empty)
sb.Append(" AND ").Append(condition);
sb.Append(" ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending));
}
else if (pageIndex > middleIndex && pageIndex < lastIndex) {
// 代码略
} else if (pageIndex >= lastIndex) {
// 代码略
}
return sb.ToString();
}
除了Paging方法还有另外几个方法:
// 根据记录总数和分页大小计算分页数。
public static int GetPageCount(int recordCount, int pageSize) {
return (int)Math.Ceiling((double)recordCount/pageSize);
}
// 计算中间页的页索引。
public static int GetMidPageIndex(int pageCount) {
return (int)Math.Ceiling((double)pageCount/2) - 1;
}
// 获取排序的方式("ASC"表示升序,"DESC"表示降序)
public static String GetSortType(bool ascending) {
return (ascending ? "ASC" : "DESC");
}
// 获取一个布尔值,该值指示排序的方式是否为升序。
public static bool IsAscending(String orderType) {
return ((orderType.ToUpper() == "DESC") ? false : true);
}
原文转自:http://www.ltesting.net