APP.CONFIG
<configuration>
<connectionStrings>
<add name="Dbconn" connectionString="Server=数据库访问地址(127.0.0.1);Database=数据库名称;User Id=数据库用户名;Password=数据库密码;" />
</connectionStrings>
</configuration>
PAGEUTIL
public class PageUtil
{
//代表:每页显示多少条记录
public int pageSize { get; set; }
//当前页(即现在是第几页)
public int pageIndex { get; set; }
//总共有多少页
public int pageCount { get; set; }
// 代表:数据库中总共有多少条记录
public int pageRow { get; set; }
//总共多少页
public int pageTotal { get; set; }
//总记录数
public int allTotal { get; set; }
public int getPageCount()
{
return this.pageCount = ((this.pageIndex - 1) * this.pageSize);
}
public int getPageTotal()
{
return this.pageTotal = (this.allTotal + this.pageSize - 1) / this.pageSize;
}
}
调用类
//操作页面分页
public List<object> GetAllExpressByPage(PageUtil pu)
{
//总共多少页
pu.pageCount = pu.getPageCount();
//传参 总记录数量
pu.pageTotal = pu.getPageTotal();
string select = "select top " + " " + pu.pageSize + " ";
string sql = select +
string.Format(@" *
from (select row_number()
over(order by id desc) as rownumber,*
from table_name) temp_row
where rownumber>@pageCount
;");
using (IDbConnection db = new SqlConnection(DbHelper.ConnectionString))
{
return db.Query<object>(sql, pu).ToList();
}
}
前端所需参数
pageSize 一页多少条
pageIndex 第几页
allTotal 总数