使用十进制特定精度与精致小巧的输出参数 [英] Using decimal with specific precision as output parameters with Dapper

查看:160
本文介绍了使用十进制特定精度与精致小巧的输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我评估小巧玲珑的定制和繁琐的代码的替代品,到目前为止,一切都非常好,有前途。但是今天早上我无意中发现了带动态参数的问题,无法找到一个解决方案。

I am evaluating Dapper as a replacement for custom and cumbersome code and so far all was very good and promising. But this morning I have stumbled on a problem with Dynamic parameters and cannot find a solution.

一个存储过程计算账户余额和可用余额为客户返回其结果在两位小数输出参数。这些小数与精密= 18和规模= 2的存储过程声明。这个过程与目前的标准方法完美的作品。但在小巧玲珑的,我不能找到一种方法来传递这些参数,并指定规模,使我得到的背面是十进制值的整数部分。

A stored procedure calculates the Account Balance and the Available Balance for a customer returning its result in two decimal output parameters. These decimals are declared in the stored procedure with Precision=18 and Scale=2. This procedure works perfectly with the current standard methods. But in Dapper I cannot find a way to pass these parameters and specify the scale so all I get back is the integer part of the decimal value.

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");
}

和这里的问题(S),还有就是通过一种方式规模求一个小数输出参数?或者是有不同的方式来实现我的目标找回准确的十进制值?

And here is the question(s), there is a way to pass the scale for a decimal output parameter? Or there is a different way to accomplish my goal to get back the exact decimal values?

推荐答案

好了,似乎没有的方式来解决这个问题(至少没有找到一个试探性的答案),所以我把这个解决方法,我已经实现了继续工作的其余部分。

Well, seems that there is no way to resolve this problem (at least none has found a tentative answer) so I put this workaround that I have implemented to continue with the rest of the work.

var args = new DynamicParameters(new { custID = customerID});
args.Add("@accnt", dbType: DbType.Single, direction: ParameterDirection.Output);
args.Add("@avail", dbType: DbType.Single, 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");



我已通过输出参数为而不是预期的类型小数。结果
这样,我猜想SqlParameter.Scale属性设置为大于零的不同的东西,我能得到小数位数当我尝试读取输出参数。结果
。如果有人有更好的解决方案,让我知道。

I have passed the output parameters as Single instead of the expected type Decimal.
In this way, I suppose the SqlParameter.Scale property is set to something different than zero and I could get the decimals digits when I try to read the output parameters.
If someone has a better solution let me know.

这篇关于使用十进制特定精度与精致小巧的输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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