我正在评估 Dapper 作为自定义和繁琐代码的替代品,到目前为止,一切都非常好,很有前途。但是今天早上我偶然发现了动态参数的问题,无法找到解决方案。
存储过程计算客户的帐户余额和可用余额,以两个十进制输出参数返回其结果。这些小数在存储过程中声明为 Precision=18 和 Scale=2。此程序与当前的标准方法完美配合。但是在 Dapper 中,我找不到传递这些参数并指定比例的方法,所以我得到的只是十进制值的整数部分。
using (IDbConnection connection = OpenConnection())
{
var args = new DynamicParameters(new { custID = customerID});
// No way to set the scale here?
args.Add("@accnt", dbType: DbType.Decimal, direction: ParameterDirection.Output);
args.Add("@avail", dbType: DbType.Decimal, direction: ParameterDirection.Output);
var results = connection.QueryMultiple("Customer_CalcBalance", args, commandType:CommandType.StoredProcedure);
decimal account = args.Get<decimal>("@accnt");
decimal availab = args.Get<decimal>("@avail");
}
Run Code Online (Sandbox Code Playgroud)
这是问题,有没有办法传递十进制输出参数的比例?或者有一种不同的方法来实现我的目标来取回精确的十进制值?
聚会有点晚了,但我想我应该提到这个问题已于 2015 年修复。这是 GitHub 问题。用法示例:
public void Issue261_Decimals()
{
var parameters = new DynamicParameters();
parameters.Add("c", dbType: DbType.Decimal, direction: ParameterDirection.Output, precision: 10, scale: 5);
connection.Execute("create proc #Issue261 @c decimal(10,5) OUTPUT as begin set @c=11.884 end");
connection.Execute("#Issue261", parameters, commandType: CommandType.StoredProcedure);
var c = parameters.Get<Decimal>("c");
c.IsEqualTo(11.884M);
}
Run Code Online (Sandbox Code Playgroud)