年前最后一问,来者有分!我用存储过程分页,我无法得到返回值,帮我看看怎么了?
存储过程的返回值是
@pageCount int = 1 output, ----查询结果分页后的总页数
@Counts int = 1 output ----查询到的记录数
print @pageCount
print @Counts
分析器输出:
3
30
----------------------------------------------
SqlParameter[] prams={
data.MakeOutParam("@pageCount",SqlDbType.Int,50),//传入返回值参数的函数
data.MakeOutParam("@Counts",SqlDbType.Int,50)
}
pageCount=(string)prams[0].Value;
Counts=(string)prams[1].Value;
if (Counts!=string.Empty)
Response.Write("返回值:"+Counts);
else
Response.Write("Nothing");
获取的值 为什么是空的 ?
郁闷!
[643 byte] By [
whfyc] at [2008-1-9]
说明一下:
网页上输出的是:返回值:
说明程序执行了 Response.Write("返回值:"+Counts); 语句!
但是什么都没有!,
怎么了?
data.MakeOutParam("@Counts",SqlDbType.Int,50)
是生与成一个 dirction为out的参数吧?
//这中间应该还有些代码吧?
pagecount =..
data.MakeOutParam("@Counts",SqlDbType.Int,50) 这里面做了什么 好好看看
你参数的类型错了吧!sqldbtype.int?应该是.varchar ,50吧!
附上我用 的存储过程:
--/*-----存储过程 分页处理 SW 2005-03-28创建 -------*/
--/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/
--/*-----存储过程 分页处理 2005-04-21修改 添加Distinct查询功能-------*/
--/*-----存储过程 分页处理 2005-05-18修改 多字段排序规则问题-------*/
--/*-----存储过程 分页处理 2005-06-15修改 多字段排序修改-------*/
--/*-----存储过程 分页处理 2005-12-13修改 修改数据分页方式为top max模式 性能有极大提高-------*/
--/*-----缺点:相对之前的not in版本主键只能是整型字段,如主键为GUID类型请使用not in 模式的版本-------*/
CREATE PROCEDURE proc_ListPageInt
(
@tblName nvarchar(200), ----要显示的表或多个表的连接
@fldName nvarchar(500) = '*', ----要显示的字段列表
@pageSize int = 10, ----每页显示的记录个数
@page int = 1, ----要显示那一页的记录
@pageCount int = 1 output, ----查询结果分页后的总页数
@Counts int = 1 output, ----查询到的记录数
@fldSort nvarchar(200) = null, ----排序字段列表或条件
@Sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(1000) = null, ----查询条件,不需where
@ID nvarchar(150), ----主表的主键
@Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句
Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句
Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句
Declare @strSortType nvarchar(10) ----数据排序规则A
Declare @strFSortType nvarchar(10) ----数据排序规则B
Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造
Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造
if @Dist = 0
begin
set @SqlSelect = 'select '
set @SqlCounts = 'Count(*)'
end
else
begin
set @SqlSelect = 'select distinct '
set @SqlCounts = 'Count(DISTINCT '+@ID+')'
end
if @Sort=0
begin
set @strFSortType=' ASC '
set @strSortType=' DESC '
end
else
begin
set @strFSortType=' DESC '
set @strSortType=' ASC '
end
--------生成查询语句--------
--此处@strTmp为取得查询结果数量的语句
if @strCondition is null or @strCondition='' --没有设置显示条件
begin
set @sqlTmp = @fldName + ' From ' + @tblName
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
set @strID = ' From ' + @tblName
end
else
begin
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end
----取得查询结果总数量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts
--取得分页总数
set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
/**//**当前页大于总页数 取最后一页**/
if @page>@pageCount
set @page=@pageCount
--/*-----数据分页2分处理-------*/
declare @pageIndex int --总数/页大小
declare @lastcount int --总数%页大小
set @pageIndex = @tmpCounts/@pageSize
set @lastcount = @tmpCounts%@pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize
--//***显示分页
if @strCondition is null or @strCondition='' --没有设置显示条件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
begin
if @page=1
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType
else
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
+' order by '+ @fldSort +' '+ @strFSortType
end
end
else
begin
set @page = @pageIndex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
else --有查询条件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
begin
if @page=1
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
else
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
+' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
end
end
else
begin
set @page = @pageIndex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
+' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
------返回查询结果-----
exec sp_executesql @strTmp
--print @strTmp
SET NOCOUNT OFF
GO
存储过程的返回值是
@pageCount int = 1 output, ----查询结果分页后的总页数
@Counts int = 1 output ----查询到的记录数
print @pageCount
print @Counts
分析器输出:
3
30
=================>可见存储过程没有问题
----------------------------------------------
SqlParameter[] prams={
data.MakeOutParam("@pageCount",SqlDbType.Int,50),//传入返回值参数的函数
data.MakeOutParam("@Counts",SqlDbType.Int,50)
}
==============>可见参数构造有点小小的问题,不过应该不影响结果MakeOutParam不知道怎么定义的,猜第三个参数应该是长度,int长度一定不等于50
====>下面有问题了
我们来排除,不可能你调用存储过程没传参==>会抛出异常
也不可能你没有执行存储过程==>党和人民相信你检查过了
最可能的一种情况是你在MakeOutParam
过程中没有将返回值SqlParameter的Direction属性指定,如果没有指定初值,也没有指定Direction
为output/return,一般也会抛出异常.但是很不幸的是你的存储过程中定义了默认值
@pageCount int = 1 output,所以你最后检查@pagecount is null
去掉存储过程的默认返回值,如果抛出异常,则你应该在MakeOutParam中加入
param.Direction=ParameterDirection.Output;
==============>
pageCount=(string)prams[0].Value;
Counts=(string)prams[1].Value;
if (Counts!=string.Empty)
Response.Write("返回值:"+Counts);
else
Response.Write("Nothing");
获取的值 为什么是空的 ?
郁闷!
===============>
新年快乐,好运同在
我的传参函数:
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
SqlParameter param;
if(Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
说明一下:
网页上输出的是:返回值:
说明程序执行了 Response.Write("返回值:"+Counts); 语句!
但是什么都没有!,
怎么了?
param.Direction = Direction;
Direction==?
怎么来的?
MakeOutParam
顾名思义,改为
param.Direction = ParameterDirection.Output;
即可
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
===>Value又怎么来的
MakeOutParam
顾名思义.去掉即可
=======>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
SqlParameter param=new SqlParameter(ParamName, DbType);
if(Size > 0)
param.Size=Size;
param.Direction = ParameterDirection.Output;
return param;
}
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if(Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
晕了,又说错了!
我用的传参函数是:
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if(Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
这个好象是最初的petshop提出来的
或者是aspnetforums?
看不出问题了
SqlConnection conn=new SqlConnection(@"server=blackant\db;database=master;uid=sa;pwd=不告诉你");
SqlCommand comm=new SqlCommand("sp_output",conn);
comm.CommandType=CommandType.StoredProcedure;
SqlParameter param=new SqlParameter("@out",SqlDbType.Int);
//1
//param.Direction=ParameterDirection.Output;
comm.Parameters.Add(param);
conn.Open();
//2
//comm.ExecuteNonQuery();
conn.Close();
Console.WriteLine("Value:{0}",param.Value);
这是一个最简单的示例,其中的1,2两个注释的地方有一个没有执行都会出现你说的情况
两步都必须存在,如果指定了param.Direction=ParameterDirection.Output;
另一个可能在没有执行了:)==>不至于吧
ps:存储过程定义如下
create procedure sp_output
@out int=1 output
as
set @out=22
ParameterDirection.Output
定义为输出参数应该就没问题了
新开贴,把分全部分了!~
http://community.Codefund.cn/Expert/topic/5271/5271936.xml?temp=.2996332
如果你在查询分析器里执行没有问题的话,请检查你每一步的代码.
是否传递了正确的参数,
是否执行了SqlCommand的ExecuteDataReader()之类的方法,
在事件探查器里看到,运行了此存储过程,并得到了结果!
事件探查器:
declare @P1 int
set @P1=19
declare @P2 int
set @P2=4
exec proc_ListPageInt @tblName = N' info_public ', @fldName = N' * ', @pageSize = 5, @page = 1, @fldSort = N' infoid ', @Sort = 1, @strCondition = N' ', @ID = N' infoid ', @Counts = @P1 output, @pageCount = @P2 output
select @P1, @P2
------------------------------------------
结果:
表1:
ID Title ...
----------------------------------------
1 kkkkkkk ...
..
..
表2:
无名列 无名列
------------------------------------
19 4
”无名列“就是输出参数的值!
我也遇到过,在你的另一个帖说明了
这里补充一下,你返回 dataset试一下 好象可以,现在不确定了 呵呵