asp.net 调用数据库的储存过程 返回的值为空解决思路
asp.net 调用数据库的储存过程 返回的值为空
数据库的储存过程:
USE [PB_BookShopDemo]
GO
/****** Object: StoredProcedure [dbo].[myFenYe11] Script Date: 2015/5/18 17:27:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[myFenYe11]
@Cateid int, --图书分类
@pageIndex int, --选中的页数
@pageSize int, --页数的大小
@pageCount int output --输出这类图书共有多少页
as
declare @paichu int
set @paichu=(@pageIndex-1)*@pageSize
declare @sql varchar(3000)
set @Cateid=convert(int,@Cateid)
select top (@pageSize) Books.Id,Title,Author,PublisherId,PublishDate,ISBN,UnitPrice,ContentDescription,TOC,CategoryId,Clicks,Categories.Name from books inner join Categories on Books.CategoryId=Categories.Id where CategoryId=(@Cateid) and Books.id not in(select top (@paichu) id from books order by PublishDate desc,id desc) order by publishDate desc ,id desc
declare @zongjilu int
set @zongjilu=(select count(0) from books where CategoryId=convert(varchar(10),@Cateid))
set @pageCount=@zongjilu/@pageSize
if(@zongjilu%@pageSize!=0)
begin
set @pageCount=@pageCount+1
end
else
begin
set @pageCount=@zongjilu/@pageSize
end
declare @dis int
exec myFenYe11 1,1,5, @dis output
select @dis
用这一步查的时候 @dis能返回值
SqlConnection sql = new SqlConnection(ConnectionString);
try
{
SqlCommand com = new SqlCommand("myFenYe11",sql);
com.CommandType = CommandType.StoredProcedure;
//com.Parameters.Add("@Cateid", SqlDbType.Int).Value = fenlei;
//com.Parameters.Add("@pageIndex", SqlDbType.Int).Value = pageIndex;
//com.Parameters.Add("@pageSize", SqlDbType.Int).Value = Pagesize;
//SqlParameter pars = new SqlParameter("@pageCount",SqlDbType.Int,4);
//pars.Direction = ParameterDirection.Output;
//com.Parameters.Add(pars);
SqlParameter[] par = new SqlParameter[]{new SqlParameter("@Cateid",fenlei),new SqlParameter("@pageIndex",pageIndex),
new SqlParameter("@pageSize",Pagesize),new SqlParameter("@pageCount",SqlDbType.Int) };
par[3].Direction = ParameterDirection.Output;
com.Parameters.AddRange(par);
sql.Open();
SqlDataReader rad = com.ExecuteReader(CommandBehavior.CloseConnection);
yesu = Convert.ToInt32(par[par.Length-1].Value); //在这里数据就是为空了
return rad;
}
------解决思路----------------------
你用sql profiler监控下呗,拿到发送到服务器的存储过程调用语句,在查询分析器执行,看有值output么?
数据库的储存过程:
USE [PB_BookShopDemo]
GO
/****** Object: StoredProcedure [dbo].[myFenYe11] Script Date: 2015/5/18 17:27:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[myFenYe11]
@Cateid int, --图书分类
@pageIndex int, --选中的页数
@pageSize int, --页数的大小
@pageCount int output --输出这类图书共有多少页
as
declare @paichu int
set @paichu=(@pageIndex-1)*@pageSize
declare @sql varchar(3000)
set @Cateid=convert(int,@Cateid)
select top (@pageSize) Books.Id,Title,Author,PublisherId,PublishDate,ISBN,UnitPrice,ContentDescription,TOC,CategoryId,Clicks,Categories.Name from books inner join Categories on Books.CategoryId=Categories.Id where CategoryId=(@Cateid) and Books.id not in(select top (@paichu) id from books order by PublishDate desc,id desc) order by publishDate desc ,id desc
declare @zongjilu int
set @zongjilu=(select count(0) from books where CategoryId=convert(varchar(10),@Cateid))
set @pageCount=@zongjilu/@pageSize
if(@zongjilu%@pageSize!=0)
begin
set @pageCount=@pageCount+1
end
else
begin
set @pageCount=@zongjilu/@pageSize
end
declare @dis int
exec myFenYe11 1,1,5, @dis output
select @dis
用这一步查的时候 @dis能返回值
SqlConnection sql = new SqlConnection(ConnectionString);
try
{
SqlCommand com = new SqlCommand("myFenYe11",sql);
com.CommandType = CommandType.StoredProcedure;
//com.Parameters.Add("@Cateid", SqlDbType.Int).Value = fenlei;
//com.Parameters.Add("@pageIndex", SqlDbType.Int).Value = pageIndex;
//com.Parameters.Add("@pageSize", SqlDbType.Int).Value = Pagesize;
//SqlParameter pars = new SqlParameter("@pageCount",SqlDbType.Int,4);
//pars.Direction = ParameterDirection.Output;
//com.Parameters.Add(pars);
SqlParameter[] par = new SqlParameter[]{new SqlParameter("@Cateid",fenlei),new SqlParameter("@pageIndex",pageIndex),
new SqlParameter("@pageSize",Pagesize),new SqlParameter("@pageCount",SqlDbType.Int) };
par[3].Direction = ParameterDirection.Output;
com.Parameters.AddRange(par);
sql.Open();
SqlDataReader rad = com.ExecuteReader(CommandBehavior.CloseConnection);
yesu = Convert.ToInt32(par[par.Length-1].Value); //在这里数据就是为空了
return rad;
}
------解决思路----------------------
你用sql profiler监控下呗,拿到发送到服务器的存储过程调用语句,在查询分析器执行,看有值output么?