SqlDataAdapter 得不到output返回值

xugan666 2014-01-28 10:44:25
代码如下
/// <summary>
/// 分页获取配置信息列表
/// </summary>
/// <param name="pageIndex">索引</param>
/// <param name="pageSize">数据条数</param>
/// <param name="DictType">类型</param>
/// <param name="TotalCount">总计数</param>
/// <returns>DataTable</returns>
public DataTable GetDictInfoList(int pageIndex, int pageSize, string dictType, out int totalCount)
{
string strSql = "DictInfoList";
List<SqlParameter> parmeters = new List<SqlParameter>();
parmeters.Add(new SqlParameter("@pageIndex", pageIndex));
parmeters.Add(new SqlParameter("@pageSize", pageSize));
parmeters.Add(new SqlParameter("@dictType", dictType));
parmeters.Add(new SqlParameter("@totalCount", ParameterDirection.Output));
return this.GetListStoredProcedure(strSql, parmeters.ToArray(), out totalCount);
}


protected DataTable GetListStoredProcedure(string sqlContent, SqlParameter[] paramters, out int totalCount)
{
//查询结果
DataTable dtData = new DataTable();

// 建立数据库连接
using (SqlConnection conn = new SqlConnection(_dBConnString))
{
//SqlCommand sc = new SqlCommand()
//{
// Connection = conn,
// CommandText = sqlContent,
// CommandType = CommandType.StoredProcedure
//};
// sc.Parameters.AddRange(paramters);
// 创建一个适配器
SqlDataAdapter adapter = new SqlDataAdapter()
{
SelectCommand = new SqlCommand()
{
Connection = conn,
CommandText = sqlContent,
CommandType = CommandType.StoredProcedure
}
};
// SqlDataAdapter adapter = new SqlDataAdapter(sc);
adapter.SelectCommand.Parameters.AddRange(paramters);

try
{

// 执行查询,并将数据导入DataSet


adapter.Fill(dtData);
// object res = sc.Parameters["@totalCount"].Value;
totalCount = (int)adapter.SelectCommand.Parameters["@totalCount"].Value; }
finally
{
// 关闭数据库连接
conn.Close();
}

}
return dtData;
}
存储过程
USE [ErpManage]
GO
/****** Object: StoredProcedure [dbo].[DictInfoList] Script Date: 01/28/2014 10:42:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[DictInfoList]
(
@pageIndex int=0,
@pageSize int =0,
@Id int=0,
@dictType varchar(4)='',
@totalCount int output
)
AS
BEGIN
Declare @totalRecord int;
Declare @strWhere nvarchar(500)='';
DECLARE @sql nvarchar(1000);
if(@Id>0)
begin
set
@strWhere=' AND Id='+ cast(@Id as CHAR)
end
if(ISNULL(@dictType,'')!='')
begin
set @strWhere +=' AND DictType='''+@DictType+''''
end

SET @sql=' select @totalRecord=COUNT(*) from DictInfo WHERE 1=1 '+@strWhere
exec sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT


--print(@totalCount)
Set @sql='Select *from (
Select ROW_NUMBER() over(order by DictType Desc ) as OrderDictType,* From DictInfo
) t where OrderDictType Between '+CAST(@pageIndex as CHAR)+' AND '+CAST(@pageSize as CHAR)+' '+@strWhere

exec (@sql)

select @totalCount=@totalRecord
return @totalCount
END

在sqlserver2008上面执行存储过程是对的,
但是用调试工具执行的结果是:exec DictInfoList @pageIndex=0,@pageSize=10,@dictType=N'',@totalCount=2
请高手解答这是为什么?
...全文
224 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xugan666 2014-02-10
  • 打赏
  • 举报
回复
引用 15 楼 qldsrx 的回复:
这是你原来的代码:parmeters.Add(new SqlParameter("@totalCount", ParameterDirection.Output)); 明明有指定ParameterDirection.Output的,看来你原来的代码都不是自己写的。
这样写并不行,我不知道你试过没有,并需要这样写。parameters[3].Value = ParameterDirection.Output;
xugan666 2014-01-28
  • 打赏
  • 举报
回复
引用 11 楼 qldsrx 的回复:
是否报错?没报错的话就该查找存储过程而不是代码,那个参数值是2?不是有值了吗?也就是说执行是对的,如果执行失败,参数值应该为空才对。
SqlParameter[] parameters = { new SqlParameter("@pageIndex", SqlDbType.Int,4), new SqlParameter("@pageSize", SqlDbType.Int,4), new SqlParameter("@dictType", SqlDbType.VarChar,4), new SqlParameter("@totalCount",SqlDbType.Int,4,) }; parameters[0].Value = pageIndex; parameters[1].Value = pageSize; parameters[2].Value = dictType; parameters[3].Value = ParameterDirection.Output; return this.GetListStoredProcedure(strSql, parameters, out totalCount); 后来我改成这样的模式,还是不行。。
xugan666 2014-01-28
  • 打赏
  • 举报
回复
引用 11 楼 qldsrx 的回复:
是否报错?没报错的话就该查找存储过程而不是代码,那个参数值是2?不是有值了吗?也就是说执行是对的,如果执行失败,参数值应该为空才对。
都没有报错,我数据库中有24条,但是他只返回2条,很奇怪,sql直接执行存储过程就是对的。
qldsrx 2014-01-28
  • 打赏
  • 举报
回复
是否报错?没报错的话就该查找存储过程而不是代码,那个参数值是2?不是有值了吗?也就是说执行是对的,如果执行失败,参数值应该为空才对。
md5e 2014-01-28
  • 打赏
  • 举报
回复
string strSql = "DictInfoList"; List<SqlParameter> parmeters = new List<SqlParameter>(); parmeters.Add(new SqlParameter("@pageIndex", 1)); parmeters.Add(new SqlParameter("@pageSize", 10)); parmeters.Add(new SqlParameter("@totalCount", 0)); parmeters[2].Direction = ParameterDirection.Output; SqlConnection conn = new SqlConnection("Integrated Security=false;uid=sa;password=123456;Persist Security Info=False;Initial Catalog=m5_lgyy_2013;Data Source=LIUCHAOLIN\\VOPCC;TimeOut=3;"); SqlDataAdapter adapter = new SqlDataAdapter(); SqlCommand Command = new SqlCommand(); Command.Connection = conn; Command.CommandText = strSql; Command.CommandType = CommandType.StoredProcedure; Command.Parameters.AddRange(parmeters.ToArray()); adapter.SelectCommand = Command; DataTable dt=new DataTable(); adapter.Fill(dt); Response.Write(Command.Parameters["@totalCount"].Value); Response.End();
xugan666 2014-01-28
  • 打赏
  • 举报
回复
引用 8 楼 liuchaolin 的回复:
然后DataSet就是有两个Table,分别是ds.Table[0],ds.Table[1]
这样做是可以,但是我觉得不合理。。虽然我不知道是什么原因导致存储过程的output值返回不会来,但是我会一直找到答案。。
md5e 2014-01-28
  • 打赏
  • 举报
回复
然后DataSet就是有两个Table,分别是ds.Table[0],ds.Table[1]
md5e 2014-01-28
  • 打赏
  • 举报
回复
还有一种做法是直接返回两个集合,一个是Select count(1),一个是Select xxx Select count(1) From ...;Select * From...
xugan666 2014-01-28
  • 打赏
  • 举报
回复
引用 5 楼 liuchaolin 的回复:
或者用ParameterDirection.ReturnValue;来接收返回值 MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue; MyCommand.ExecuteNonQuery(); Response.Write(MyCommand.Parameters["@return"].Value.ToString());
这个我都试过,话说你这个方法是影响行数吧,我这个是返回一个数据集合,然后再返回一个output。。。
md5e 2014-01-28
  • 打赏
  • 举报
回复
或者用ParameterDirection.ReturnValue;来接收返回值 MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue; MyCommand.ExecuteNonQuery(); Response.Write(MyCommand.Parameters["@return"].Value.ToString());
xugan666 2014-01-28
  • 打赏
  • 举报
回复
引用 3 楼 liuchaolin 的回复:
output是不需要 return @totalCount set @totalCount=xxx就可以了
还是不行,我改成你这样了,返回结果还是2.
md5e 2014-01-28
  • 打赏
  • 举报
回复
output是不需要 return @totalCount set @totalCount=xxx就可以了
xugan666 2014-01-28
  • 打赏
  • 举报
回复
@版主呢。。
xugan666 2014-01-28
  • 打赏
  • 举报
回复
求高手解决啊。。。。。
qldsrx 2014-01-28
  • 打赏
  • 举报
回复
这是你原来的代码:parmeters.Add(new SqlParameter("@totalCount", ParameterDirection.Output)); 明明有指定ParameterDirection.Output的,看来你原来的代码都不是自己写的。
xugan666 2014-01-28
  • 打赏
  • 举报
回复
引用 10 楼 liuchaolin 的回复:
string strSql = "DictInfoList"; List<SqlParameter> parmeters = new List<SqlParameter>(); parmeters.Add(new SqlParameter("@pageIndex", 1)); parmeters.Add(new SqlParameter("@pageSize", 10)); parmeters.Add(new SqlParameter("@totalCount", 0)); parmeters[2].Direction = ParameterDirection.Output; SqlConnection conn = new SqlConnection("Integrated Security=false;uid=sa;password=123456;Persist Security Info=False;Initial Catalog=m5_lgyy_2013;Data Source=LIUCHAOLIN\\VOPCC;TimeOut=3;"); SqlDataAdapter adapter = new SqlDataAdapter(); SqlCommand Command = new SqlCommand(); Command.Connection = conn; Command.CommandText = strSql; Command.CommandType = CommandType.StoredProcedure; Command.Parameters.AddRange(parmeters.ToArray()); adapter.SelectCommand = Command; DataTable dt=new DataTable(); adapter.Fill(dt); Response.Write(Command.Parameters["@totalCount"].Value); Response.End();
恩,你这个方法可以的,我发现非要指定 parmeters[2].Direction = ParameterDirection.Output;

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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