简体   繁体   English

Oracle编号为C#decimal

[英]Oracle number to C# decimal

I know there are several threads and posts regarding this issue in the internet and I've read them (not every article, I have to admit) but none of them did fully satisfy me. 我知道在互联网上有几个关于这个问题的帖子和帖子我已经阅读过了(不是每篇文章,我都要承认)但是没有一个能完全满足我。

My situation: 我的情况:
I'm using ODP.net (dll version 2.111.6.0) to access the Oracle DB (version 10 + 11) and a DataReader to retrieve the data (.NET 3.5, C#). 我正在使用ODP.net(dll版本2.111.6.0)访问Oracle DB(版本10 + 11)和DataReader来检索数据(.NET 3.5,C#)。

Using this code results in a ' System.OverflowException (Arithmetic operation resulted in an overflow.) ' 使用此代码会导致' System.OverflowException(算术运算导致溢出)


decimal.TryParse(oraReader.GetOracleDecimal(0).Value.ToString(), 
  NumberStyles.Any, null, out parsedOraDecimal)

and this one results in a value of ' 3,000000000000000000000000000000000000000000000000000000000E-126 ' 这一个导致值为' 3,0000000000000000000000000000000000000000000000000000000000000E-126 '


decimal.TryParse(oraReader.GetOracleValue(0).ToString(), 
  NumberStyles.Any, null, out parsedOraDecimal)

Now I have to find some way to retrieve and evaluate this value properly - the DB is also used from other apps which are out of my control so changes there are not possible. 现在我必须找到一些方法来正确检索和评估这个值 - 数据库也可以从我无法控制的其他应用程序中使用,因此无法进行更改。

Converting the types in my C# code from 'decimal' to 'double' is also not really an option. 将我的C#代码中的类型从'decimal'转换为'double'也不是一个真正的选择。

Any ideas? 有任何想法吗?

OracleDecimal has a larger precision than decimal. OracleDecimal具有比decimal更大的精度。 For that reason, you have to reduce the precision first. 因此,您必须首先降低精度。 Forget all the parsing, use implicit conversion. 忘记所有解析,使用隐式转换。 Try something along the lines of (untested): 尝试一下(未经测试的):

decimal d = (decimal)(OracleDecimal.SetPrecision(oraReader.GetOracleDecimal(0), 28));

I just had a similar issue, and tried the approach of changing the OracleDataAdapter to return Oracle specific types ( data_adapter.ReturnProviderSpecificTypes = true; ), but this is just a PITA, you end up casting OracleStrings back to strings, etc. 我刚刚遇到了类似的问题,并尝试了更改OracleDataAdapter以返回Oracle特定类型(data_adapter.ReturnProviderSpecificTypes = true;)的方法,但这只是一个PITA,最终将OracleStrings转换回字符串等。

In the end I solved it by doing the precision rounding in the SQL statement using Oracle's round function: 最后,我通过使用Oracle的round函数在SQL语句中进行精确舍入来解决它:

SELECT round( myfield, 18 ) FROM mytable

Dotnet will then happily convert the figure to a decimal. 然后,Dotnet会愉快地将数字转换为小数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM