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

数据库
sql server数据库是功能性相对来说完善了的,在数据库市场中也是领军的佼佼者,在sql server存储过程中涉及到的翻页操作过程是怎样的呢?下文中将为大家带来详细的解析。

sql server存储过程的翻页是sql server数据库操作中重要的环节之一,下文中就在Access中模拟sql server存储过程翻页的过程,供大家参考。

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 + 1Create 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;
}
}

上文中涉及到的代码比较多,看起来可能大家会觉得没有头绪,所以大家要静下心来,认真阅读文章中的知识,相信大家都能够从中收获。

【编辑推荐】

  1. 如何使用Access数据库压缩文件
  2. Access数据库成功导入Oracle库方法
  3. Microsoft SQL Server数据库库名的介绍
  4. SQL Server数据库简体繁体数据混用的问题
责任编辑:迎迎 来源: iTbulo.COM
相关推荐

2009-03-03 11:51:54

微软数据库ACCESS

2010-08-31 15:39:25

DB2存储过程

2009-08-06 16:44:06

2010-07-15 12:38:14

SQL Server存

2010-11-12 09:18:13

SQL Server存

2011-03-24 13:38:47

SQL Server 存储分页

2010-11-12 09:46:55

Sql Server存

2010-09-14 10:16:57

sql server

2010-09-14 10:36:23

sql server存

2011-03-28 10:46:36

sql server存储分页

2011-07-14 13:38:34

2011-08-15 15:56:31

SQL Server

2010-07-06 14:06:52

SQL Server存

2010-11-10 13:03:15

SQL Server存

2010-07-05 10:06:51

SQL Server扩

2010-06-28 09:21:04

SQL Server存

2010-09-06 11:24:32

SQL Server语句

2010-09-03 15:08:03

SQLselect语句

2011-08-12 14:51:31

SQL ServerSET NOCOUNT

2010-09-02 09:37:36

SQL删除
点赞
收藏

51CTO技术栈公众号