How to do MS Access database paging + search?

余生长醉 提交于 2019-12-28 06:53:12

问题


I have a MS Access 2003 database with a table called product1 with a Primary key named Product Code. There is no auto id column.

I have used this sql to do the custom data paging.

 SELECT *
FROM (
  SELECT Top 1  -- = PageSize
  *
  FROM
  (
   SELECT TOP 1  -- = StartPos + PageSize
   *
   FROM product1
   ORDER BY product1.[Product Code]
  ) AS sub1
  ORDER BY sub1.[Product Code] DESC
 ) AS clients
ORDER BY [Product Code]

Now my problem is Search. When I search for something on the database table and point it.

How can I make sure still paging works fine?


回答1:


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)



来源:https://stackoverflow.com/questions/6914637/how-to-do-ms-access-database-paging-search

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