How to do MS Access database paging + search?

一曲冷凌霜 提交于 2019-11-28 01:41:24

I'm querying Access from C# as well (with paging and searching), and I'm using the following code to build all my queries:

var sb = new StringBuilder();
sb.Append("select {0} from {1}");
sb.Append(" where {3} in (");
sb.Append("select top {4} sub.{3}");
sb.Append("    from (");
sb.Append("          select top {5} tab.{3}");
sb.Append("          from {1} tab");
sb.Append("          where {2}");
sb.Append("          order by tab.{3}");
sb.Append("    ) sub");
sb.Append("    order by sub.{3} desc");
sb.Append(")");
sb.Append("order by {3}");

sql = string.Format(sb.ToString(), this.ColumnsToSelect, this.TableName, 
    this.WhereClause, this.OrderBy, this.PageSize, this.PageNum * this.PageSize);

Note that in order for this to work, all parameters must be supplied
(if you don't actually want to filter anything, just put 1=1 into the WHERE clause)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!