--使用系统存储过程实现的通用分页存储过程(转自邹建)
CREATE PROC sp_PageView
@sql ntext, --要执行的sql语句
@PageCurrent int, --要显示的页码
@PageSize int, --每页的大小
@PageCount int OUTPUT --总页数
AS
SET NOCOUNT ON
DECLARE @p1 int
--初始化分页游标
EXEC sp_cursoropen
@cursor=@p1 OUTPUT,
@stmt=@sql,
@scrollopt=1,
@ccopt=1,
@rowcount=@PageCount OUTPUT--计算总页数
IF ISNULL(@PageSize,0)<1
SET @PageSize=10
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
IF ISNULL(@PageCurrent,0)<1 OR ISNULL(@PageCurrent,0)>@PageCount
SET @PageCurrent=1
ELSE
SET @PageCurrent=(@PageCurrent-1)*@PageSize+1--显示指定页的数据
EXEC sp_cursorfetch @p1,16,@PageCurrent,@PageSize--关闭分页游标
EXEC sp_cursorclose @p1GO
我用的以下方法取结果
public GetCategories(string sqlstr, int pcur, int psize)
{
dstruct bs = new dstruct();
IList<dInfo> categories = new List<dInfo>();
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter("@sql",SqlDbType.NText),
new SqlParameter("@PageCurrent", SqlDbType.Int,4),
new SqlParameter("@PageSize", SqlDbType.Int,4),
new SqlParameter("@PageCount", SqlDbType.Int,4)
};
parms[0].Value = sqlstr;
parms[1].Value = pcur;
parms[2].Value = psize;
parms[3].Direction = ParameterDirection.Output; using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.StoredProcedure, "sp_PageView", parms))
{
rdr.NextResult();
while (rdr.Read())
{
dInfo cat = new dInfo(Convert.ToInt32(rdr.GetValue(0)), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3));
categories.Add(cat);
}
bs.bi = categories;
bs.pagecount = Convert.ToInt32(parms[3].Value);
}
return bs;
}
返回bs中的bs.bi是rdr成功获取,但是bs.pagecount没能获取,请大家指点,感谢!
直接取 parms[3].Direction.Value的值就是你返回的值
大家对C#不是很熟,建议楼主去C#版问一下吧。
因为测试存储过程时应取不到返回值declare @total_pagesize int
exec sp_PageView 'select * from info',1,10,@total_pagesize
print '总页数:'+convert(varchar,@total_pagesize) --该行执行后根本无任何显示
go返回的结果是两个记录集,第一个是创建分页游标时产生的空结果集,第二是我要的记录集,但是@PageCount int OUTPUT --总页数取不到呀?