Using decimal with specific precision as output parameters with Dapper

落花浮王杯 提交于 2019-12-01 20:08:11

A little late to the party, but I thought I would mention that this was fixed in 2015. Here is the GitHub issue. Example usage:

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

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

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.

I had the same issue. I changed my USP or query to pass varhcar(10) and assign varchar(10) to decimal value with success!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!