解析来自DataReader的小数 [英] Parsing a decimal from a DataReader

查看:86
本文介绍了解析来自DataReader的小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了解决此错误的方法,但是现在真的很奇怪为什么会发生这种情况,我想知道是否还有其他人遇到了此错误.

I found a workaround for this error, but am now really curious as to why this would be happening, was wondering if anyone else has had this error.

我的功能如下:

public void Blog_GetRating(int blogID, ref decimal rating, ref int voteCount)
{
    // Sql statements
    // Sql commands

    if (DataReader.Read())
    {
        // this line throws a 'Input string was not in a correct format.' error.
        rating = decimal.Parse(DataReader["Rating"].ToString());

        // this works absolutly fine?!
        decimal _rating = 0;
        decimal.TryParse(DataReader["Rating"].ToString(), out _rating);

        rating = _rating;
    }
}

有人见过吗?

如果我输入以下内容,那就更奇怪了:

What's even weirder is if i type this:

rating = decimal.Parse("4.0");

效果很好,4.0是我的DataReader推出的.

that works fine, the 4.0 is what is coming out from my DataReader.

就像我之前说的那样,TryParse方法可以正常工作,因此不会阻止我继续前进,但是现在我真的很想知道是否有人对此有答案.

As I said previous, the TryParse method works fine so it's not stopping me from carrying, but now I'm really interested to see if anyone has an answer for it.

期待一些答复!

塞恩

编辑-已解决

decimal.Parse方法运行良好,第二次运行该函数(处于循环中),未对帖子进行评级,因此数据读取器返回空值.在SQL中将COALESCE包装起来可以很好地解决问题.因此,正如您所说,为什么tryparse方法没有引发异常,而只将默认值0保留为_rating.

The decimal.Parse method was working fine, the second time the function was running (was in a loop), a post hadn't been rated so a null value was being returned by the data reader. Wrapping COALESCE round my calculation in SQL solved the problem fine. Hence why, as you said, the tryparse method wasn't throwing an exception, just keeping the default of 0 to _rating.

推荐答案

这对我来说似乎并不奇怪.

That doesn't look weird to me at all.

Decimal.Parse()应该 引发格式错误的异常. Decimal.TryParse()不会引发该异常,而只是返回false.更重要的是,您不是要检查Decimal.TryParse()的返回值.我会给您带来真正的好机会,即Decimal.TryParse()对于每个导致Decimal.Parse()异常的输入都返回false,在其他任何地方都是如此.并且Decimal.TryParse()返回false时,输出参数始终只是"0".

Decimal.Parse() is supposed to throw an exception for bad formats. Decimal.TryParse() will not throw that exception, but instead just return false. The kicker is that you're not checking the return value from Decimal.TryParse(). I'll give you real good odds that Decimal.TryParse() returns false for every input that causes an exception with Decimal.Parse(), and true everywhere else. And when Decimal.TryParse() returns false, the output argument is always just "0".

一个可能的警告是本地化.如果Decimal.Parse()抱怨看似正常的输入,则可以检查服务器上使用的数字格式(当前区域性)是否使用逗号而不是十进制数来将系数与尾数区分开.但是考虑到您的"4.0"测试效果很好,我怀疑这是问题所在.

The one possible caveat is localization. If Decimal.Parse() is complaining about a seemingly normal input, you might check if the number format (current culture) used on your server uses a comma rather than a decimal to separate the coefficient from the mantissa. But given your "4.0" test worked fine, I doubt this is the problem.

最后,从数据读取器执行此转换时,应考虑数据读取器的源列类型.如果可能已经是小数.为什么将其转换为仅将其转换回字符串的字符串?

Finally, when doing this conversion from data reader you should consider the source column type of the data reader. If might already be a decimal. Why convert it to a string only to convert it back?

这篇关于解析来自DataReader的小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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