sql server 2012新特性, 支持 OFFSET/FETCH分页, 就像mysql的limit, 比之前的各种top舒服多了, 看各位大佬们的测评文章说效率也是不相上下的,
有时候写个小工具或者临时处理一些数据, 就不需要ef这种大工具出场了, 所以我临时拼了一个方法来用, 配合dapper还挺简单的.
1 //假装using Dapper; public IEnumerable<Books> GetBooks(int pageIndex, int pageSize) { var conn = GetSqlConnection(); var sql = "select * from books"; var pagingSql = (sql , " id desc ", pageIndex, pageSize); var rowCount = conn.ExecuteScalar<int>($"select count(*) from ({sql}) x"); var pagedList = conn.Query<Books>(pagingSql); } public SqlConnection GetSqlConnection() { //假装返回一个connection对象 return conn; } public string PagingSql(string sql, string orderby, int page, int pagesize) { if (page <= 0) { page = 0; } else { page--; } var offset = page * pagesize; var _sql = $@"SELECT x.* from ( {sql} ) x ORDER BY {orderby} OFFSET {offset} ROW FETCH NEXT {(pagesize)} ROW ONLY"; return _sql; }
如果您觉得这篇博客对您产生了一些必要的帮助, 欢迎您对我意思意思, 我将会觉得您非常够意思!