命令上的强制转换异常无效.Parameters.GetValue.
本文关键字:GetValue Parameters long 无效 异常 转换 命令 | 更新日期: 2023-09-27 18:36:46
我有一些 ADO.Net 代码如下,它在数据库中成功执行了SP,同时在tenantId抛出异常,请指导我。
command.Parameters.Input.BigInt(ParameterConstants.TenantId)
.BigInt(ParameterConstants.PartyId, tenant.PartyId)
.Varchar(ParameterConstants.TenantCode, tenant.TenantCode)
.BigInt(ParameterConstants.PharmaId, tenant.PharmaId)
.BigInt(ParameterConstants.UserId, userProfileId);
command.ExecuteNonQuery();
var tenantId = command.Parameters.GetValue<long>(ParameterConstants.TenantId);
在var tenantId
它抛出投射异常System.InvalidCastExceptio
...确切的例外Specified cast is not valid.
An exception of type 'System.InvalidCastException' occurred in ...Data.dll but was not handled in user code
Additional information: Specified cast is not valid.
您的GetValue<long>
方法可能没有处理NULL
值。该GetValue<>
可能是您或其他人编写的自定义方法(不是Microsoft),因此我不知道如何解决它。你可以试试:
var tenantId = command.Parameters.GetValue<long?>(ParameterConstants.TenantId);
我的猜测是数据库类型和 TenantId 的类类型不兼容。例如,字符串 vs int vs long。