VB.NET 调用以一个分页存储过程怎么得不到合计页数呢?请帮助。

蛋蛋の忧伤 2011-05-21 03:42:33

这是我调用存储过程的方法


'调用分页的存储过程
Public Function GetSQLDataTable_Proc_forFenYe2(ByVal sql, ByVal PageCurrent, ByVal PageSize, ByVal PageCount) As DataSet
Try
'定义命令对象,并使用储存过程
Dim myCommand As New SqlClient.SqlCommand
myCommand.CommandType = CommandType.StoredProcedure
myCommand.CommandText = "sp_PageView"
Dim conn As New SqlClient.SqlConnection
conn.ConnectionString = ModuleDB.getConnectSQL_forTransaction_module
myCommand.Connection = conn
'定义一个数据适配器,并设置参数
Dim myDapter As New SqlClient.SqlDataAdapter(myCommand)
myDapter.SelectCommand.Parameters.Add("@sql", SqlDbType.Text).Value = sql
myDapter.SelectCommand.Parameters.Add("@PageCurrent", SqlDbType.Int).Value = PageCurrent
myDapter.SelectCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize

myDapter.SelectCommand.Parameters.Add("@PageCount", SqlDbType.Int)
myDapter.SelectCommand.Parameters.Item("@PageCount").Direction = ParameterDirection.Output‘设置为返回值
Dim cc As Integer = myDapter.SelectCommand.Parameters.Item("@PageCount").Value
MsgBox("合计页数:" & cc) '这是怎么是0??

Dim ds As New DataSet
Try
myDapter.Fill(ds)
MsgBox(ds.Tables.Count)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return ds
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function

请大家帮着看看我上面的方法为什么不行
--------------------

下面是我在查询分析器里的语句是可以查询到正确的内容的
下面的语句一共返回3个结果集 第一个是空的,第2个是第12页的数据第3个结果集合是移动多少页
declare @P_count int
exec sp_PageView
@sql='select * from buserinfo',@PageCurrent=12,@PageSize=10,@PageCount=@P_count out
SELECT @P_count

----------------------------






下面是我用的别人的一个存储过程
/*
--利用SQL未公开的存储过程实现分页
方法简单且效率高,已知的问题就是要多返回一个空的记录集
解决的方法是在前台调用时,用set recordset=recordset.nextrecordset
的方法跳过第一个记录集
此方法由J9988提供,改成了方便调用的存储过程
--邹建2004.05(引用请保留此信息)--*/


--缺点是返回2张表,第一张是空表,第二张才有数据

/*--调用示例
declare @PageCount int
exec sp_PageView
@sql='select * from sysobjects',
@PageCurrent=2,
@PageCount=@PageCount out
SELECT @PageCount
--*/
Create PROC [dbo].[sp_PageView]
@sql ntext, --要执行的sql语句
@PageCurrent int=1, --要显示的页码
@PageSize int=10, --每页的大小
@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 @p1
...全文
44 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧