gpt4 book ai didi

c# - Dapper 如何通过 DynamicParameter.Get 处理 DBNull

转载 作者:行者123 更新时间:2023-11-30 12:42:42 28 4
gpt4 key购买 nike

var param = new DynamicParameters();
param.Add("MsgId", DbType.Int32, direction: ParameterDirection.Output);

connection.Execute(MessageSelOutMessageId, param, commandType: CommandType.StoredProcedure);
count = param.Get<int>("MsgId");

通过引用 Dapper,我使用上面的代码调用了一个带有输出参数 - MsgId 的存储过程。它工作正常,但在某些情况下,存储过程不会返回任何值,并且返回的输出参数值为空。在这些情况下,我得到了这个异常(exception):

Attempting to cast a DBNull to a non nullable type! Note that out/return parameters will not have updated values until the data stream completes (after the 'foreach' for Query(..., buffered: false), or after the GridReader has been disposed for QueryMultiple)

我明白我们可以通过使用下面的代码将返回数据类型标记为可为空以避免此错误

count = param.Get<int?>("MsgId");

但是,有没有其他方法可以检查 param.Get("MsgId") == null 而不是使用可空数据类型 - int?

最佳答案

感谢 Paulius,尝试使用动态数据类型 count = param.Get<dynamic>("MsgId");它符合我的要求。

关于c# - Dapper 如何通过 DynamicParameter.Get 处理 DBNull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32856508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com