簡単にmssql accessデータベーストップの改ページ方法を話します。
uusing system.collections.generic;
sing system;
using system.text;
/// <summary>
/// sql
/// </summary>
public static class paginghelper
{
/// <summary>
/// sql ,
/// </summary>
/// <param name="_recordcount"> </param>
/// <param name="_pagesize"> </param>
/// <param name="_pageindex"> </param>
/// <param name="_safesql">sql </param>
/// <param name="_orderfield"> , “,” </param>
/// <returns> sql </returns>
public static string createpagingsql(int _recordcount, int _pagesize, int _pageindex, string _safesql, string _orderfield)
{
// ,
string[] arrstrorders = _orderfield.split(new char[] { ',' }, stringsplitoptions.removeemptyentries);
stringbuilder sboriginalorder = new stringbuilder(); //
stringbuilder sbreverseo rder = new stringbuilder(); // ,
for (int i = 0; i < arrstrorders.length; i++)
{
arrstrorders[i] = arrstrorders[i].trim(); //
if (i != 0)
{
sboriginalorder.append(", ");
sbreverseorder.append(", ");
}
sboriginalorder.append(arrstrorders[i]);
int index = arrstrorders[i].indexof(" "); //
if (index > 0)
{
// ,
bool flag = arrstrorders[i].indexof(" desc", stringcomparison.ordinalignorecase) != -1;
sbreverseorder.appendformat("{0} {1}", arrstrorders[i].remove(index), flag ? "asc" : "desc");
}
else
{
sbreverseorder.appendformat("{0} desc", arrstrorders[i]);
}
}
//
_pagesize = _pagesize == 0 ? _recordcount : _pagesize;
int pagecount = (_recordcount + _pagesize - 1) / _pagesize;
//
if (_pageindex < 1)
{
_pageindex = 1;
}
else if (_pageindex > pagecount)
{
_pageindex = pagecount;
}
stringbuilder sbsql = new stringbuilder();
// , top n,
if (_pageindex == 1)
{
sbsql.appendformat(" select top {0} * ", _pagesize);
sbsql.appendformat(" from ({0}) as t ", _safesql);
sbsql.appendformat(" order by {0} ", sboriginalorder.tostring());
}
// , top n
else if (_pageindex == pagecount)
{
sbsql.append(" select * from ");
sbsql.append(" ( ");
sbsql.appendformat(" select top {0} * ", _recordcount - _pagesize * (_pageindex - 1));
sbsql.appendformat(" from ({0}) as t ", _safesql);
sbsql.appendformat(" order by {0} ", sbreverseorder.tostring());
sbsql.append(" ) as t ");
sbsql.appendformat(" order by {0} ", sboriginalorder.tostring());
}
//
else if (_pageindex < (pagecount / 2 + pagecount % 2))
{
sbsql.append(" select * from ");
sbsql.append(" ( ");
sbsql.appendformat(" select top {0} * from ", _pagesize);
sbsql.append(" ( ");
sbsql.appendformat(" select top {0} * ", _pagesize * _pageindex);
sbsql.appendformat(" from ({0}) as t ", _safesql);
sbsql.appendformat(" order by {0} ", sboriginalorder.tostring());
sbsql.append(" ) as t ");
sbsql.appendformat(" order by {0} ", sbreverseorder.tostring());
sbsql.append(" ) as t ");
sbsql.appendformat(" order by {0} ", sboriginalorder.tostring());
}
//
else
{
sbsql.appendformat(" select top {0} * from ", _pagesize);
sbsql.append(" ( ");
sbsql.appendformat(" select top {0} * ", ((_recordcount % _pagesize) + _pagesize * (pagecount - _pageindex)));
sbsql.appendformat(" from ({0}) as t ", _safesql);
sbsql.appendformat(" order by {0} ", sbreverseorder.tostring());
sbsql.append(" ) as t ");
sbsql.appendformat(" order by {0} ", sboriginalorder.tostring());
}
return sbsql.tostring();
}
/// <summary>
/// sql
/// </summary>
/// <param name="_n"> </param>
/// <param name="_safesql">sql </param>
/// <returns> sql </returns>
public static string createtopnsql(int _n, string _safesql)
{
return string.format(" select top {0} * from ({1}) as t ", _n, _safesql);
}
/// <summary>
/// sql
/// </summary>
/// <param name="_safesql">sql </param>
/// <returns> sql </returns>
public static string createcountingsql(string _safesql)
{
return string.format(" select count(1) as recordcount from ({0}) as t ", _safesql);
}
}
です。