连接Access数据库,提取数据分页
假设PageSize为10,记录总数为52,则总分页数为6页。 middleIndex=2,firstIndex=0,lastIndex=5.
pageIndex= 0 进第一页
pageIndex=1 进前半段
pageIndex= 3 进后半段
pageIndex= 5 进最后
核心代码段:
/***查询语句***/#region/***查询语句***/
/**//// <summary>
/// 获取根据指定字段排序并分页查询的 SELECT 语句。
/// </summary>
/// <param name="pageSize">每页要显示的记录的数目。</param>
/// <param name="pageIndex">要显示的页的索引。</param>
/// <param name="recordCount">数据表中的记录总数。</param>
/// <param name="tableName">要查询的数据表。</param>
/// <param name="queryFields">要查询的字段。</param>
/// <param name="primaryKey">主键字段。</param>
/// <param name="ascending">是否为升序排列。</param>
/// <param name="condition">查询的筛选条件。</param>
/// <returns>返回排序并分页查询的 SELECT 语句。</returns>
public static String Paging(
int pageSize,
int pageIndex,
int recordCount,
String tableName,
String queryFields,
String primaryKey,
bool ascending,
String condition )
{
实现#region 实现
StringBuilder sb = new StringBuilder();
int pageCount = GetPageCount(recordCount,pageSize); //分页的总数
int middleIndex = GetMidPageIndex(pageCount); //中间页的索引
int firstIndex = 0; //第一页的索引
int lastIndex = pageCount - 1; //最后一页的索引
@PageIndex = @FirstIndex#region @PageIndex <= @FirstIndex
//Top @PageSize取到第一页要显示的记录 select top @PageSize @QueryFields Form @TableName WHERE @Condition --ORDER BY @PrimaryKey ASC
if (pageIndex <= firstIndex)
{
sb.Append("SELECT TOP ").Append(pageSize).Append(" ").Append(queryFields)
.Append(" FROM ").Append(tableName);
if (condition != String.Empty)
sb.Append(" WHERE ").Append(condition);
sb.Append(" ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending));
}
#endregion
@FirstIndex @PageIndex = @MiddleIndex#region @FirstIndex < @PageIndex <= @MiddleIndex
//取前半部分记录的SQL语句。 先取出当前页之前 的所有记录的主键值,再从中选出最大值,然后取出主键值 > 该最大值的前@PageSize条记录。
// select top @PageSize @QueryFields from @TableName where @PrimaryKey>(
//select Max(@PrimaryKey) from
//(select top @PageSize*@PageIndex @PrimaryKey from @TableName where @Condition) TableA
//) where @Condition
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));
}
#endregion
@MiddleIndex @PageIndex @LastIndex#region @MiddleIndex < @PageIndex < @LastIndex
//取数据表后半部分记录的sql语句。先取出当前页之后的所有记录的主键值,再从中选出最小值,然后取出主键值 <该最小值 的前@PageSize条记录。
//select * from(select Top @PageSize @QueryFields From @TableName where @PrimaryKey <(
//select min(@PrimaryKey) from (
//select top (@RecordCount-@PageSize*(@PageIndex+1)) @PrimaryKey from @TableName where Condition order by @primaryKey DESC ) TableA
//) where @Condition order by @primaryKey DESC) TableB Order by @Primarykey ASC
else if (pageIndex > middleIndex && pageIndex < lastIndex)
{
sb.Append("SELECT * FROM ( SELECT TOP ")
.Append(pageSize).Append(" ").Append(queryFields)
.Append(" FROM ").Append(tableName)
.Append(" WHERE ").Append(primaryKey);
if (ascending)
sb.Append(" < (").Append(" SELECT MIN(");
else
sb.Append(" > (").Append(" SELECT MAX(");
sb.Append(primaryKey).Append(") FROM ( SELECT TOP ")
.Append(recordCount-pageSize*(pageIndex+1)).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))
.Append(" ) TableB ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending));
}
#endregion
@PageIndex = @LastIndex#region @PageIndex >= @LastIndex
//取最后一页的记录时要先计算出该页的记录数,作为Top语句的条件,而不能直接用Top @PageSize ,这样取出来的记录 并不只是 最后一页的。其实很多网站确实这样做。
//select * from(
//select top (@RecordCount -@PageSize*@LastIndex) @QueryFields From @TableName Where @Condition Order by @PrimaryKey DESC
// ) TableA order by @Primarykey ASC
else if (pageIndex >= lastIndex)
{
sb.Append("SELECT * FROM ( SELECT TOP ").Append(recordCount-pageSize*lastIndex)
.Append(" ").Append(queryFields)
.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 ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending));
}
#endregion
return sb.ToString();
#endregion
}
#endregion
/**//// <summary>
/// 获取根据指定字段排序并分页查询的 SELECT 语句。
/// </summary>
/// <param name="pageSize">每页要显示的记录的数目。</param>
/// <param name="pageIndex">要显示的页的索引。</param>
/// <param name="recordCount">数据表中的记录总数。</param>
/// <param name="tableName">要查询的数据表。</param>
/// <param name="queryFields">要查询的字段。</param>
/// <param name="primaryKey">主键字段。</param>
/// <param name="ascending">是否为升序排列。</param>
/// <param name="condition">查询的筛选条件。</param>
/// <returns>返回排序并分页查询的 SELECT 语句。</returns>
public static String Paging(
int pageSize,
int pageIndex,
int recordCount,
String tableName,
String queryFields,
String primaryKey,
bool ascending,
String condition )
{
实现#region 实现
StringBuilder sb = new StringBuilder();
int pageCount = GetPageCount(recordCount,pageSize); //分页的总数
int middleIndex = GetMidPageIndex(pageCount); //中间页的索引
int firstIndex = 0; //第一页的索引
int lastIndex = pageCount - 1; //最后一页的索引
@PageIndex = @FirstIndex#region @PageIndex <= @FirstIndex
//Top @PageSize取到第一页要显示的记录 select top @PageSize @QueryFields Form @TableName WHERE @Condition --ORDER BY @PrimaryKey ASC
if (pageIndex <= firstIndex)
{
sb.Append("SELECT TOP ").Append(pageSize).Append(" ").Append(queryFields)
.Append(" FROM ").Append(tableName);
if (condition != String.Empty)
sb.Append(" WHERE ").Append(condition);
sb.Append(" ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending));
}
#endregion
@FirstIndex @PageIndex = @MiddleIndex#region @FirstIndex < @PageIndex <= @MiddleIndex
//取前半部分记录的SQL语句。 先取出当前页之前 的所有记录的主键值,再从中选出最大值,然后取出主键值 > 该最大值的前@PageSize条记录。
// select top @PageSize @QueryFields from @TableName where @PrimaryKey>(
//select Max(@PrimaryKey) from
//(select top @PageSize*@PageIndex @PrimaryKey from @TableName where @Condition) TableA
//) where @Condition
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));
}
#endregion
@MiddleIndex @PageIndex @LastIndex#region @MiddleIndex < @PageIndex < @LastIndex
//取数据表后半部分记录的sql语句。先取出当前页之后的所有记录的主键值,再从中选出最小值,然后取出主键值 <该最小值 的前@PageSize条记录。
//select * from(select Top @PageSize @QueryFields From @TableName where @PrimaryKey <(
//select min(@PrimaryKey) from (
//select top (@RecordCount-@PageSize*(@PageIndex+1)) @PrimaryKey from @TableName where Condition order by @primaryKey DESC ) TableA
//) where @Condition order by @primaryKey DESC) TableB Order by @Primarykey ASC
else if (pageIndex > middleIndex && pageIndex < lastIndex)
{
sb.Append("SELECT * FROM ( SELECT TOP ")
.Append(pageSize).Append(" ").Append(queryFields)
.Append(" FROM ").Append(tableName)
.Append(" WHERE ").Append(primaryKey);
if (ascending)
sb.Append(" < (").Append(" SELECT MIN(");
else
sb.Append(" > (").Append(" SELECT MAX(");
sb.Append(primaryKey).Append(") FROM ( SELECT TOP ")
.Append(recordCount-pageSize*(pageIndex+1)).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))
.Append(" ) TableB ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending));
}
#endregion
@PageIndex = @LastIndex#region @PageIndex >= @LastIndex
//取最后一页的记录时要先计算出该页的记录数,作为Top语句的条件,而不能直接用Top @PageSize ,这样取出来的记录 并不只是 最后一页的。其实很多网站确实这样做。
//select * from(
//select top (@RecordCount -@PageSize*@LastIndex) @QueryFields From @TableName Where @Condition Order by @PrimaryKey DESC
// ) TableA order by @Primarykey ASC
else if (pageIndex >= lastIndex)
{
sb.Append("SELECT * FROM ( SELECT TOP ").Append(recordCount-pageSize*lastIndex)
.Append(" ").Append(queryFields)
.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 ORDER BY ").Append(primaryKey).Append(" ")
.Append(GetSortType(ascending));
}
#endregion
return sb.ToString();
#endregion
}
#endregion