sql server 2012 Page / dapper / C # spell sql / free during storage / Easy

sql server 2012 new features and support for OFFSET / FETCH pagination, like the mysql limit, all kinds of top comfortable than before and more, you see the bigwigs of the evaluation of the article said that efficiency is also comparable, 

Sometimes write a small tool or a temporary deal with some of the data, we do not need such large tool ef played, so I fight a temporary method to use, with the dapper quite simple.

 

 

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;
}

 

Guess you like

Origin www.cnblogs.com/adinet/p/10983659.html