开发者

How to do MS Access database paging + search?

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?


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜