C#, DB

利用Sql对DataTable分页

数据分页有很多种,但是大多都是先加载所有的数据,然后根据数据分页,但是个人觉得效率不高,我比较喜欢的方法是基于数据表本身的容量分页,查出需要分页的数据即可,对了,是基于MSSql,Function:

public DataTable GetPageRecords(int Start, int Size, out int Count, string NoOrderByQuery, string UniquOrderbyKeys)
{
string sql = @”select * from
(
select ROW_NUMBER() OVER({0}) AS n00000000,

c00000000.* from ({1}) c00000000

) t00000000 where n00000000 >= {2} and n00000000 <={3}”;

int End = Size + Start – 1;

sql = string.Format(sql, UniquOrderbyKeys, NoOrderByQuery, Start, End);

DataTable dt = GetRecords(sql);

sql = string.Format(” select count(1) n from ({0}) t “, NoOrderByQuery);
DataTable dc = GetRecords(sql);
if (dc != null && dc.Rows.Count > 0)
{
Count = Convert.ToInt32(dc.Rows[0][“n”]);
}
else
{
Count = 0;
}
return dt;
}

Pls call me CPP.
Posts created 150

发表评论

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top