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

php中文网
发布: 2016-06-07 15:12:11
原创
1137人浏览过

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 in

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 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 GetPagedPost(PagedPost p, out int TotalRecords)
        {
            List list = new List();

            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 IDs = new List(); //获取主题ID列表
                conn.Open();
                using (OleDbDataReader dr = MyComm.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        IDs.Add((int)dr[0]);
                   
                    }
                }
              
                TotalRecords=IDs.Count;//返回记录总数
                if (TotalRecords                     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                     {
                        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;
            }
         }

 

 转帖请注明出处..深Q

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号