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

2016-02-19 18:20 6 1 收藏

想不想get新技能酷炫一下,今天图老师小编就跟大家分享个简单的在Access中模拟sql server存储过程翻页教程,一起来看看吧!超容易上手~

【 tulaoshi.com - 编程语言 】

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

            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);
                Listint IDs = new Listint(); //获取主题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.Length1)
                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;
            }
         }

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

 转帖请注明出处..深Q

来源:http://www.tulaoshi.com/n/20160219/1617341.html

延伸阅读
标签: SQLServer
  SQLSERVER估计是为了安装或者其它方面,它内置了一批危险的存储过程。能读到注册表信息,能写入注册表信息,能读磁盘共享信息等等……各位看到这儿,心里可能会在想,我的网站中有其它的代码,又不像查询分析器那样能直接将结果输出。给你这个权限,也不能怎么样,还是看不到信息。如果各位这样想就大错特错了。提示一下,如果攻击...
标签: Web开发
下面介绍一个JSP调用SQL Server存储过程的实例: 创建表: CREATE TABLE [BookUser] (     [UserID] [int] IDENTITY (1, 1) NOT NULL ,     [UserName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,     [Title] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,   &...
--有输入参数的存储过程--create proc GetComment(@commentid int)asselect * from Comment where CommentID=@commentid --有输入与输出参数的存储过程--create proc GetCommentCount@newsid int,@count int outputasselect @count=count(*) from Comment where NewsID=@newsid --返回单个值的函数--create function MyFunction(@newsid int)...
标签: ASP
  建立表: CREATE TABLE [TestTable] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [FirstName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [LastName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Note] [nvarchar] (2000) COLLATE Chinese_PRC_CI_...
sql Server提供了系统存储过程sp_procoption,这个存储过程可以用于当SQL Server服务启动时指派一个或者多个存储过程自动执行。这是一个很不错的选择,它可以用于多种多样的用途。比如,你可能在你的数据库中有开销很大的查询,这个查询在首次执行时会花费一些时间。通过使用sp_procoption,你可以在服务器启动时运行这个查询以此来预先编译执行...

经验教程

1000

收藏

89
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部