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

本文详细介绍了在SQLServer中使用存储过程进行翻页查询的具体实现方式,并提供了在Access中因不支持存储过程而采取的替代方案,通过程序逻辑实现了类似的功能。
sql server中翻页存储过程:
None.gifCreate           PROC blog_GetPagedPosts
None.gif(
None.gif 
@PageIndex int,
None.gif 
@PageSize int,
None.gif 
@BlogID   int=0,
None.gif 
@PostType int=-1,
None.gif  
@CategoryID int=-1,
None.gif  
@Hiding     bit =0,
None.gif  
@Count    int output 
None.gif        
None.gif)
None.gif
as
None.gif
DECLARE @PageLowerBound int
None.gif
DECLARE @PageUpperBound int
None.gif
SET @PageLowerBound = @PageSize * @PageIndex - @PageSize
None.gif
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
None.gif
None.gif
Create Table #IDs
None.gif(
None.gif TempID 
int IDENTITY (11NOT NULL,
None.gif EntryID 
int not null
None.gif)
None.gif
Insert  into #IDs(EntryID)  select DISTINCT [ID] from view_Content  where CategoryID=@CategoryID and blogID=@BlogID   order by [ID] desc
None.gif
SELECT  vc.*
None.gif
FROM   View_Content vc 
None.gif     
INNER JOIN #IDS tmp ON (vc .[ID] = tmp.EntryID)
None.gif
WHERE  tmp.TempID > @PageLowerBound 
None.gif 
AND tmp.TempID < @PageUpperBound and vc.Hiding=0
None.gif
ORDER BY tmp.TempID
None.gif
SELECT @Count=COUNT(*FROM  #IDS 
None.gif
SELECT @Count=COUNT(*FROM  #IDS 
None.gif
DROP TABLE #IDS
None.gif
return @Count
None.gif
GO
None.gif
None.gif
None.gif

在Access中由于不支持存储过程,不能建立临时表只能在程序中实现
Access中实现如下,这也是我在myblog Access版中使用的:
None.gifpublic List<DayBook> GetPagedPost(PagedPost p, out int TotalRecords)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            List
<DayBook> list = new List<DayBook>();
InBlock.gif
InBlock.gif            
using (OleDbConnection conn = GetOleDbConnection())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StringBuilder sql 
= new StringBuilder();
InBlock.gif                sql.AppendFormat(
"select  [ID] from blog_Content as p ");//构造查询条件
InBlock.gif
                if (p.CategoryID > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    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);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sql.AppendFormat(
" where p.blogID={0} ", p.BlogID);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (p.PostType != PostType.Undeclared)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sql.AppendFormat(
" and p.PostType={0} ", (int)p.PostType);
ExpandedSubBlockEnd.gif                }

InBlock.gif                sql.Append(
" order by p.[DateUpdated] desc");
InBlock.gif               
// NetDiskContext.Current.Context.Response.Write(sql.ToString());
InBlock.gif                
//NetDiskContext.Current.Context.Response.End();
InBlock.gif
                OleDbCommand MyComm = new OleDbCommand(sql.ToString(), conn);
InBlock.gif                List
<int> IDs = new List<int>(); //获取主题ID列表
InBlock.gif
                conn.Open();
InBlock.gif                
using (OleDbDataReader dr = MyComm.ExecuteReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        IDs.Add((
int)dr[0]);
InBlock.gif                    
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif               
InBlock.gif                TotalRecords
=IDs.Count;//返回记录总数
InBlock.gif
                if (TotalRecords < 1)
InBlock.gif                    
return list;
InBlock.gif                
int pageLowerBound = p.PageSize * p.PageIndex - p.PageSize;//记录索引
InBlock.gif
                int pageUpperBound = pageLowerBound + p.PageSize ; 
InBlock.gif                StringBuilder sb 
= new StringBuilder();
InBlock.gif                
if (TotalRecords >= pageLowerBound)
InBlock.gif                    
for (int i = pageLowerBound; i < TotalRecords && i < pageUpperBound; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.AppendFormat(
"{0},", IDs[i]);//构造ID in() 条件,取其中一页
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                
else return list; //如没有记录返回空表
InBlock.gif
                if(sb.Length>1)
InBlock.gif                sb.Remove(sb.Length 
- 11);//删除最后一个逗号
InBlock.gif
            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());
InBlock.gif                
using (OleDbDataReader dr = MyComm.ExecuteReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        list.Add(DataHelp.LoadDayBook(dr));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return list;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif         }

None.gif
None.gif


 转帖请注明出处..深Q

转载于:https://www.cnblogs.com/windinwing/archive/2006/06/10/422576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值