当前位置: 首页 > 图文教程 > 数据库 > Access > 在Access中模拟sql server存储过程翻页

Access
长期使用中型Access数据库的一点经验与缺点
加密你的Access数据库asp打开方法
access下如何恢复已经删除的记录;如何恢复已经删除的表、窗体等等对象
恢复从 Access 2000、 Access 2002 或 Access 2003 中数据库删除表的方法
ACCESS的参数化查询,附VBSCRIPT(ASP)和C#(ASP.NET)函数
access的备注字段限制64K
根据IP跳转到用户所在城市的实现步骤
Access出现"所有记录中均未找到搜索关键字"的错误解决
Access数据库出现“无法保存;正被别的用户锁定”的原因
ACCESS 调用后台存储过程的实现方法
Access 模糊参数 分页查询
Access转Sql Server问题 实例说明
Access 执行SQL的方法
access 数据库自启动困难解决方法
ADODB连接access是出现 80004005 错误的解决方法
处理加了密码的MDB文件
ACCESS数据访问页配置实例
Word与Access数据交流技巧
ACCESS作为网站数据库的弊端
Oracle与Access表之间的导入和导出实现

在Access中模拟sql server存储过程翻页


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 47 ::
收藏到网摘: n/a

sql server中翻页存储过程:
Create           PROC blog_GetPagedPosts
(
 @PageIndex int,
 @PageSize int,
 @BlogID   int=0,
 @PostType int=-1,
  @CategoryID int=-1,
  @Hiding     bit =0,
  @Count    int output
       
)
as
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
SET @PageLowerBound = @PageSize * @PageIndex - @PageSize
SET @PageUpperBound = @PageLowerBound + @PageSize + 1

Create Table #IDs
(
 TempID int IDENTITY (1, 1) NOT NULL,
 EntryID int not null
)
Insert  into #IDs(EntryID)  select DISTINCT [ID] from view_Content  where CategoryID=@CategoryID and blogID=@BlogID   order by [ID] desc
SELECT  vc.*
FROM   View_Content vc
     INNER JOIN #IDS tmp ON (vc .[ID] = tmp.EntryID)
WHERE  tmp.TempID > @PageLowerBound
 AND tmp.TempID < @PageUpperBound and vc.Hiding=0
ORDER BY tmp.TempID
SELECT @Count=COUNT(*) FROM  #IDS
SELECT @Count=COUNT(*) FROM  #IDS
DROP TABLE #IDS
return @Count
GO

在Access中由于不支持存储过程,不能建立临时表只能在程序中实现
Access中实现如下,这也是我在myblog Access版中使用的:
public List<DayBook> GetPagedPost(PagedPost p, out int TotalRecords)
        {
            List<DayBook> list = new List<DayBook>();

            using (OleDbConnection conn = GetOleDbConnection())
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat("select  [ID] from blog_Content as p ");//构造查询条件
                if (p.CategoryID > 0)
                {
                    sql.AppendFormat(",blog_Categories AS c, blog_Links AS l WHERE c.CategoryID=l.CategoryID and (p.ID=l.PostID ) and c.CategoryID={1} and p.BlogID={0}  ",p.BlogID, p.CategoryID);
                }
                else
                {
                    sql.AppendFormat(" where p.blogID={0} ", p.BlogID);
                }
                if (p.PostType != PostType.Undeclared)
                {
                    sql.AppendFormat(" and p.PostType={0} ", (int)p.PostType);
                }
                sql.Append(" order by p.[DateUpdated] desc");
               // NetDiskContext.Current.Context.Response.Write(sql.ToString());
                //NetDiskContext.Current.Context.Response.End();
                OleDbCommand MyComm = new OleDbCommand(sql.ToString(), conn);
                List<int> IDs = new List<int>(); //获取主题ID列表
                conn.Open();
                using (OleDbDataReader dr = MyComm.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        IDs.Add((int)dr[0]);
                   
                    }
                }
              
                TotalRecords=IDs.Count;//返回记录总数
                if (TotalRecords < 1)
                    return list;
                int pageLowerBound = p.PageSize * p.PageIndex - p.PageSize;//记录索引
                int pageUpperBound = pageLowerBound + p.PageSize ;
                StringBuilder sb = new StringBuilder();
                if (TotalRecords >= pageLowerBound)
                    for (int i = pageLowerBound; i < TotalRecords && i < pageUpperBound; i++)
                    {
                        sb.AppendFormat("{0},", IDs[i]);//构造ID in() 条件,取其中一页
                    }
                else return list; //如没有记录返回空表
                if(sb.Length>1)
                sb.Remove(sb.Length - 1, 1);//删除最后一个逗号
            MyComm.CommandText = string.Format("SELECT b.* , c.Account as Account FROM blog_Content b, Blog_Config  c where b.BlogID=c.BlogID and b.[ID] in ({0}) order by b.dateadded desc", sb.ToString());
                using (OleDbDataReader dr = MyComm.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        list.Add(DataHelp.LoadDayBook(dr));
                    }
                }
                return list;
            }
         }