当前位置:编程学习 > C#/ASP.NET >>

为什我不能获得储存过程 ouput参数的值!


-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetBookPages]
     @CategoryId int ,--where条件
 @PageSize int =10,    --页尺寸
     @PageIndex  int=1, --页码
    
     @SumCount  int output, --返回记录总数
     @PageCount int output, --返回总页数
     @OrderType  bit=0 --设置排序类型
AS
BEGIN

SET NOCOUNT ON;
    declare @strSQL varchar(5000) --主SQL语句 
    declare @tmpSQL varchar(150)  --临时SQL语句
    declare @strOrder  varchar (400) --排序类型 
    declare @tbName varchar(450) --表名
    declare @fileName  varchar(1000) --查询字段
    declare @fIdName varchar(400) --排序字段
    declare @strWhere varchar(800) --条件语句
    DECLARE @MinMaxfileName varchar(50) --最小或最大的判断字段
    
SET @MinMaxfileName='PKId'
set @tbName='Items as i,Books as b,  ItemCategory as c, ItemType as t, Publishers as p'
set @fileName='i.PKId, i.TypeId, b.PublisherId, b.ISBN, i.ImageFileSpec, i.Name as Title,i.Description,i.UnitPrice, i.UnitCost, t.Name as ItemType,  p.Name  as PublisherName '
set @fIdName='i.PKId'  
set @strWhere='c.CategoryId='+str(@CategoryId)+' AND i.PKId=c.ItemId AND b.ItemId=i.PKId AND p.PKId=b.PublisherId  AND t.PKId=i.TypeId'
--设置排序类型
 if @OrderType!=0
    begin
      set @tmpSQL='<(select min'
      set @strOrder='order by '+@fIdName+' desc'
   end
 else
    begin
      set @tmpSQL='>(select max'
      set @strOrder='order by '+@fIdName+' asc'
    end
--如果是查询第一页的就直接输出
 if @PageIndex=1
  begin
  set @strSQL='select top '+ str(@PageSize)+' '+@fileName+' from '+@tbName+' where '+@strWhere+' '+ @strOrder 
  end
 else   --如果不是查询的不是第一页的数据
   begin
   set @strSQL='select top '+ str(@PageSize)+' '+@fileName+'from '+@tbName+' where '+ @fIdName+'  '+@tmpSQL+'('+@MinMaxfileName+') from ( select Top'+str((@PageIndex-1)*@PageSize)+' '+@fIdName+' from '+@tbName+' where ' +@strWhere+' ' +@strOrder+') as tbTmp  ) and '+@strWhere+' '+@strOrder
   end 
exec (@strSQL)   


declare  @sqls nvarchar(4000) 
 
 set @sqls='select @a=count(*) from  '+@tbName+' where '+@strWhere 
 exec sp_executesql @sqls,N'@a int output',@SumCount output  --总记录数

 select @PageCount=case when @SumCount>0 then @SumCount/@PageSize+1 else 0 end --总页数


END

***************
      调试数据存储过程时,@SumCount  和 @PageCount 都是有值的
***************

************  调用存储过程   ***********************************
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("GetBookPages", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
...
//输出参数设置
SqlParameter Counts = new SqlParameter(@SumCount, SqlDbType.Int);
Counts.Direction = ParameterDirection.Output;
 da.SelectCommand.Parameters.Add(Counts);
 SqlParameter Pages = new SqlParameter(@PageCount, SqlDbType.Int);
 Pages.Direction = ParameterDirection.Output;
 da.SelectCommand.Parameters.Add(Pages);
int a = Convert.ToInt32(Counts.Value.ToString());  ---这里都是0
int b = Convert.ToInt32(Pages.Value.ToString());  ---这里都是0 --------------------编程问答--------------------  你在调用存储过程的时候 方法的参数也要用out类型的! --------------------编程问答-------------------- 你执行一下看看
出查出来有几个表
--------------------编程问答--------------------
                            
没有行受影响。
(返回 5 行)
@SumCount = 21
@PageCount = 5
@RETURN_VALUE = 0

分页数据都正常,总页数,和数据总数都有! 就是取不出来!
@RETURN_VALUE = 0 这个是什么意思
--------------------编程问答-------------------- 因为你那输的数据的另一个结果表里面 --------------------编程问答-------------------- OUT --------------------编程问答-------------------- OUT --------------------编程问答-------------------- //输出参数设置 
SqlParameter Counts = new SqlParameter(@SumCount, SqlDbType.Int); 
Counts.Direction = ParameterDirection.Output; 
da.SelectCommand.Parameters.Add(Counts); 
SqlParameter Pages = new SqlParameter(@PageCount, SqlDbType.Int); 
Pages.Direction = ParameterDirection.Output; 
da.SelectCommand.Parameters.Add(Pages); 
int a = Convert.ToInt32(Counts.Value.ToString());  ---这里都是0 
int b = Convert.ToInt32(Pages.Value.ToString());  ---这里都是0
---------------------
先执行存储过程,再获取值。。。如:


//输出参数设置 
SqlParameter Counts = new SqlParameter(@SumCount, SqlDbType.Int); 
Counts.Direction = ParameterDirection.Output; 
da.SelectCommand.Parameters.Add(Counts); 
SqlParameter Pages = new SqlParameter(@PageCount, SqlDbType.Int); 
Pages.Direction = ParameterDirection.Output; 
da.SelectCommand.Parameters.Add(Pages); 
//这里执行操作。类似:da.ExecuteNonQuery()
int a = Convert.ToInt32(Counts.Value.ToString());  ---这里都是0 
int b = Convert.ToInt32(Pages.Value.ToString());  ---这里都是0
--------------------编程问答-------------------- 你得先执行存储过程才来获取吧,不然都是默认值的
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,