您的位置:IT教程网首页>数据库教程>access>在Access模拟sqlserver存储过程翻页

在Access模拟sqlserver存储过程翻页



您正在看的acce教程是:在Acce模拟sqlserver存储过程翻页。

sqlserver中翻页存储过程:
CreatePROCblog_GetPagedPosts
(
@PageIndexint,
@PageSizeint,
@BlogIDint=0,
@PostTypeint=-1,
@CategoryIDint=-1,
@Hidingbit=0,
@Countintoutput

)
as
DECLARE@PageLowerBoundint
DECLARE@PageUerBoundint
SET@PageLowerBound=@PageSize*@PageIndex-@PageSize
SET@PageUerBound=@PageLowerBound @PageSize 1

CreateTable#IDs
(
TempIDintIDENTITY(1,1)NOTNULL,
EntryIDintnotnull
)
Iertinto#IDs(EntryID)selectDISTINCT[ID]fromview_ContentwhereCategoryID=@CategoryIDandblogID=@BlogIDorderby[ID]desc
SELECTvc.*
FROMView_Contentvc
IERJOIN#IDStmpON(vc.[ID]=tmp.EntryID)
WHEREtmp.TempIDgt;@PageLowerBound
ANDtmp.TempIDlt;@PageUerBoundandvc.Hiding=0
ORDERBYtmp.TempID
SELECT@Count=COUNT(*)FROM#IDS
SELECT@Count=COUNT(*)FROM#IDS
DROPTABLE#IDS
return@Count
GO

在Acce中由于不支持存储过程,不能建立临时表只能在程序中实现
Acce中实现如下,这也是我在myblogAcce版中使用的:
publicListlt;DayBookgt;GetPagedPost(PagedPostp,outintTotalRecords)
{
Listlt;DayBookgt;list=newListlt;DayBookgt;();

using(OleDbCoectionco=GetOleDbCoection())
{
StringBuildersql=newStringBuilder();
sql.AendFormat("select[ID]fromblog_Contentasp");//构造查询条件
if(p.CategoryIDgt;0)
{
sql.AendFormat(",blog_CategoriesASc,blog_LinksASlWHEREc.CategoryID=l.CategoryIDand(p.ID=l.PostID)andc.CategoryID={1}andp.BlogID={0}",p.BlogID,p.CategoryID);
}
else
{
sql.AendFormat("wherep.blogID={0}",p.BlogID);
}
if(p.PostType!=PostType.Undeclared)
{
sql.AendFormat("andp.PostType={0}",(int)p.PostType);
}
sql.Aend("orderbyp.[DateUpdated]desc");
//NetDi


您正在看的acce教程是:在Acce模拟sqlserver存储过程翻页。skContext.Current.Context.Reoe.Write(sql.ToString());
//NetDiskContext.Current.Context.Reoe.End();
OleDbCommandMyComm=newOleDbCommand(sql.ToString(),co);
Listlt;intgt;IDs=newListlt;intgt;();//获取主题ID列表
co.Open();
using(OleDbDataReaderdr=MyComm.ExecuteReader())
{
while(dr.Read())
{
IDs.Add((int)dr[0]);

}
}

TotalRecords=IDs.Count;//返回记录总数
if(TotalRecordslt;1)
returnlist;
intpageLowerBound=p.PageSize*p.PageIndex-p.PageSize;//记录索引
intpageUerBound=pageLowerBound p.PageSize;
StringBuilder=newStringBuilder();
if(TotalRecordsgt;=pageLowerBound)
for(inti=pageLowerBound;ilt;TotalRecordsamamilt;pageUerBound;i )
{
.AendFormat("{0},",IDs[i]);//构造IDin()条件,取其中一页
}
elsereturnlist;//如没有记录返回空表
if(.Lengthgt;1)
.Remove(.Length-1,1);//删除最后一个逗号


MyComm.CommandText=string.Format("SELECTb.*,c.AccountasAccountFROMblog_Contentb,Blog_Configcwhereb.BlogID=c.BlogIDandb.[ID]in({0})orderbyb.dateaddeddesc",.ToString());

上一篇[1][2][3]下一篇


您正在看的acce教程是:在Acce模拟sqlserver存储过程翻页。using(OleDbDataReaderdr=MyComm.ExecuteReader())
{
while(dr.Read())
{
list.Add(DataHelp.LoadDayBook(dr));
}
}
returnlist;
}
}

转帖请注明出处..深Q

上一篇[1][2][3]