|
public ActionResult Index()
{
var pagingInfo = new PagingInfoViewModel
{
SortField = "ID",
SortDirection = "DESC",
PageIndex = 0,
PageSize = ConfigHelper.PageSize
};
ViewBag.PagingInfo = pagingInfo;
var list = AA.GetDataList(pagingInfo, string.Empty);
return View(list);
}
var dt = DBHelper.GetPagedDataTable(pagingInfo, sql);
return ConvertUtil<AA>.ConvertToModel(dt);
自己写了一个方法,估计高版本有现成的分页 方法吧?
/// <summary>
/// 数据库分页
/// </summary>
/// <returns></returns>
public static DataTable GetPagedDataTable(PagingInfoViewModel pagingInfo, string sql)
{
DataTable dt = new DataTable();
try
{
int pageIndex = pagingInfo.PageIndex;
int pageSize = pagingInfo.PageSize;
string sqlCount = string.Format("select count(0) from ({0}) a ", sql);
pagingInfo.RecordCount = Convert.ToInt32(DBHelper.ExecuteScalar(sqlCount));
Int16 totalRecord = Convert.ToInt16(pagingInfo.RecordCount);
int rowStart = (pageIndex * pageSize) + 1;//当前页的起始序号
int rowEnd = (pageIndex + 1) * pageSize;//当前页的结束序号
rowEnd = rowEnd > totalRecord ? totalRecord : rowEnd;
//排序
if (!string.IsNullOrEmpty(pagingInfo.SortField))
{
if (string.IsNullOrEmpty(pagingInfo.SortDirection)) pagingInfo.SortDirection = "ASC";
sql += " ORDER BY " + pagingInfo.SortField + " " + pagingInfo.SortDirection;
}
//查询语句
sql = @"SELECT * FROM (
SELECT A.*, ROWNUM RN
FROM(" + sql + @") A
WHERE ROWNUM <= " + rowEnd.ToString() + @"
) WHERE RN >= " + rowStart.ToString();
dt = DBHelper.GetDataFromDB(sql);
//DataTable source = DBHelper.ExecuteDataTable(sql);
}
catch(Exception ex)
{
LogClass.WriteLog(ex, sql);
}
return dt;
}
|
|