我们正在使用 Dapper.Net广泛并对此感到非常满意。但是,我们在尝试使用 multi.Read 从存储过程中检索输出参数时遇到了一个问题:
var p = new DynamicParameters();
p.Add("@id", id);
p.Add("TotalRows", dbType: DbType.Int32, direction: ParameterDirection.Output);
using (var multi = cnn.QueryMultiple(string.Format("dbo.[{0}]", spName), p,
commandType: CommandType.StoredProcedure))
{
int TotalRows = p.Get<int>("@TotalRows"); //This is always null
var results = multi.Read<New_Supplier>().ToList<IDBSupplier>();
var areas = multi.Read<node>().ToList<IDBNode>();
var categories = multi.Read<node>().ToList<IDBNode>();
int TotalRows = multi.Read<int>().FirstOrDefault(); // This works
}
但是,如果我们使用 connection.Query 语法来获取单个结果集,则会填充输出参数:
var result = cnn.Query<New_Supplier>(string.Format("spname"), p,
commandType: CommandType.StoredProcedure).ToList<New_Supplier>();
int TotalRows = p.Get<int>("@TotalRows");
报错是Dapper DynamicParameters中输出参数上AttachedParam的sqlValue一直为null。
为了解决这个问题,我们将输出参数的值添加到存储过程的结果集中,并以这种方式读取它。
为什么参数总是null?
最佳答案
在 TDS 流中,OUT
参数在最后。在你的Query<T>
例如,您使用了 TDS 流:
var result = cnn.Query<New_Supplier>(string.Format("spname"), p,
commandType: CommandType.StoredProcedure).ToList<New_Supplier>();
int TotalRows = p.Get<int>("@TotalRows");
所以因为您已经使用了流,所以已经达到了新的参数值,并且您应该具有可用的值。在QueryMultiple
例如,您还没有到达 TDS 流的末尾。尝试移动 p.Get
:
var p = new DynamicParameters();
p.Add("@id", id);
p.Add("TotalRows", dbType: DbType.Int32, direction: ParameterDirection.Output);
using (var multi = cnn.QueryMultiple(string.Format("dbo.[{0}]", spName), p,
commandType: CommandType.StoredProcedure))
{
var results = multi.Read<New_Supplier>().ToList<IDBSupplier>();
var areas = multi.Read<node>().ToList<IDBNode>();
var categories = multi.Read<node>().ToList<IDBNode>();
}
int TotalRows = p.Get<int>("@TotalRows");
如果那不起作用,请告诉我;p
关于c# - multi.Read 的输出参数始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13047896/