输出参数总是带着multi.Read空 [英] Output parameter is always null with multi.Read

查看:101
本文介绍了输出参数总是带着multi.Read空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Dapper.Net 广泛,而且非常满意。然而,我们尝试使用检索存储过程的输出参数时所遇到的问题 multi.Read

We are using Dapper.Net extensively and are very happy with it. However we have come across an issue when trying to retrieve output parameters from stored procedures using 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 语法来得到一个结果集填充输出参数:

However, if we use the connection.Query syntax to get a single resultset the output parameter is populated:

var result = cnn.Query<New_Supplier>(string.Format("spname"), p,
    commandType: CommandType.StoredProcedure).ToList<New_Supplier>();
int TotalRows = p.Get<int>("@TotalRows");



该错误是AttachedParam对短小精悍DynamicParameters输出参数的sqlValue总是空。

The error is that the sqlValue of AttachedParam on the output parameter in the Dapper DynamicParameters is always null.

要解决此我们已经加入了输出参数的结果集的存储过程的价值和阅读这种方式。

To work around this we have added the value of the output parameter to the result sets of the stored procedure and are reading it that way.

为什么参数总是空?

推荐答案

在一个TDS流时, OUT 参数的的。在你的查询< T> 例如,您有消耗的TDS流的:

In a TDS stream, the OUT parameters are at the end. In your Query<T> example, you have consumed the TDS stream:

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

so because you have consumed the stream, the new parameter values have been reached, and you should have the values available. In the QueryMultiple example, you have not reached the end of the TDS stream. Try moving the 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

If that doesn't work, let me know ;p

这篇关于输出参数总是带着multi.Read空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆