在Access中模拟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;
}
}
上文中涉及到的代码比较多,看起来可能大家会觉得没有头绪,所以大家要静下心来,认真阅读文章中的知识,相信大家都能够从中收获。
【编辑推荐】
相关文章
- 从本体论开始说起——运营商关系图谱的构建及应用
- 如何成为一名数据科学家?
- 从未见过的堂兄杀了人,你的DNA是关键证据
- 20个安全可靠的免费数据源,各领域数据任你挑
- 20个安全可靠的免费数据源,各领域数据任你挑
- 阿里云李飞飞:All in Cloud时代,云原生数据库优势明显
- 基于Hadoop生态系统的一高性能数据存储格式CarbonData(性能篇)
- 大数据告诉你:10年漫威,到底有多少角色
- TigerGraph:实时图数据库助力金融风控升级
- Splunk利用Splunk Connected Experiences和Splunk Business Flow 扩大数据访问
- 大数据开发常见的9种数据分析手段
- 以免在景区看人,我爬了5W条全国景点门票数据...
- 【实战解析】基于HBase的大数据存储在京东的应用场景
- 数据科学家告诉你哪些计算机科学书籍是你应该看的
- Kafka作为大数据的核心技术,你了解多少?
- Spring Boot 整合 Redis 实现缓存操作
- 大数据学习必须掌握的五大核心技术有哪些?
- 基于Antlr在Apache Flink中实现监控规则DSL化的探索实践
- 甲骨文再次被Gartner评为分析型数据管理解决方案魔力象限领导者
- 爬取吴亦凡微博102118条转发数据,扒一扒流量的真假