日期:2014-05-17 浏览次数:20795 次
/// <summary> /// 生成top max分页sql语句,支持多表联查(事理) /// </summary> /// <param name="tableName">表名</param> /// <param name="fields">字段</param> /// <param name="primaryKey">主键</param> /// <param name="pageSize">一页显示多少条</param> /// <param name="currentPage">当前页数</param> /// <param name="where">where条件,无需加where</param> /// <param name="orderBy">order by条件,无需加order by</param> /// <returns>分页sql语句</returns> public static string GetDataPaginationSql(string tableName, string fields, string primaryKey, int pageSize, int currentPage, string where, string orderBy) { //SELECT TOP 10 UserId,AddUserId,Term,SchoolYear FROM SL_StudentsCQECDetail //WHERE CQECId > //( // SELECT MAX(CQECId) FROM // ( // SELECT TOP 10 CQECId FROM SL_StudentsCQECDetail where UserId=479 ORDER BY CQECId // ) AS T //) //and UserId=479 //order by UserId if (currentPage > 0) currentPage--; StringBuilder sql = new StringBuilder(); sql.Append("SELECT TOP " + pageSize + " " + fields + " FROM " + tableName); if (currentPage == 0) { if (!string.IsNullOrEmpty(where)) sql.Append(" WHERE " + where); } else { sql.Append(" WHERE " + primaryKey + ">"); sql.Append("(SELECT MAX(" + primaryKey + ") FROM "); sql.Append("(SELECT TOP " + pageSize * currentPage + " " + primaryKey + " FROM " + tableName); if (!string.IsNullOrEmpty(where)) sql.Append(" WHERE " + where); sql.Append(" ORDER BY " + primaryKey + ") AS T)"); if (!string.IsNullOrEmpty(where)) sql.Append(" AND " + where); } if (!string.IsNullOrEmpty(orderBy)) sql.Append(" ORDER BY " + orderBy); return sql.ToString(); }
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;