System.OverflowException: 从数据库读取 Decimal 时发生’Conversion overflows.’异常。

huangapple go评论88阅读模式
英文:

System.OverflowException: 'Conversion overflows.' during reading Decimal from DB

问题

在数据库中,我有一个相当大的值:888512650000.0。

我可以使用SqlBulkCopy将其存储在数据库中 - 没有问题。

但是,当我尝试使用SqlCommand -> SqlDataReader读取这个值时,我会遇到错误:

> System.OverflowException: 转换溢出

即使我使用简单的:

var whatever = reader["ColName"];

这个值也可以轻松地通过例如decimal.Parse("888512650000.0")解析,没有溢出问题。

数据库中的列定义:

 ColName Numeric(31, 17)

如何在C#中从数据库中读取这个值?

PS:我读过其他类似的问题,但没有回答我的问题。

引发异常的示例代码:

using SqlConnection connection = new("connection_string");
await connection.OpenAsync();

using SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT ColName FROM Table"

using SqlDataReader reader = await command.ExecuteReaderAsync();

while (await reader.ReadAsync())
{
    var whatever = reader["ColName"]; //在此处引发异常
}

示例有问题的值:

888512650000.0

完整异常:

> System.OverflowException: 转换溢出。 在 System.Data.SqlClient.SqlBuffer.get_Decimal() 在 System.Data.SqlClient.SqlBuffer.get_Value() 在 System.Data.SqlClient.SqlBuffer.get_String() 在 System.Data.DataReaderExtensions.GetString(DbDataReader reader, String name) 在 Program.<Main>$(String[] args) in C:\Users\...\source\repos\Program.cs:line 30

我已经尝试使用GetDecimal()GetString()检索此列 - 每个调用都导致相同的异常(每个调用的调用堆栈略有不同,当然)。

英文:

I've got fairly big value in DB: 888512650000.0.

I was able to store it in the database using SqlBulkCopy - no issues there.

But when I'm trying to read this value using SqlCommand -> SqlDataReader, I get an error:

> System.OverflowException: Conversion overflows

The exception is thrown even if I'm using simple:

var whatever = reader[&quot;ColName&quot;];

The value can easily be parsed to decimal using eg. decimal.Parse(&quot;888512650000.0&quot;), there's no overflow issue.

Column definition in the database:

 ColName Numeric(31, 17)

How can I read this value from DB using C#?

PS: I read other, similar questions, but there weren't replies that would answer my question.

Sample code that throws the exception:

using SqlConnection connection = new(&quot;connection_string&quot;);
await connection.OpenAsync();

using SqlCommand command = connection.CreateCommand();
command.CommandText = &quot;SELECT ColName FROM Table&quot;

using SqlDataReader reader = await command.ExecuteReaderAsync();

while (await reader.ReadAsync())
{
    var whatever = reader[&quot;ColName&quot;]; //exception thrown here
}

Example problematic value:

888512650000.0

Full exception:

> System.OverflowException: Conversion overflows. at System.Data.SqlClient.SqlBuffer.get_Decimal() at System.Data.SqlClient.SqlBuffer.get_Value() at System.Data.SqlClient.SqlBuffer.get_String() at System.Data.DataReaderExtensions.GetString(DbDataReader reader, String name) at Program.<Main>$(String[] args) in C:\Users\...\source\repos\Program.cs:line 30

I've already tried to retrieve this columns using
GetDecimal() and GetString() - each resulted in the same exception (call stack was a little bit different for each of calls, of course)

答案1

得分: 0

避免使用 System.Data.SqlClient,而是使用 Microsoft.Data.SqlClient 包。它可以处理问题中提到的值。

英文:

After whole day of searching and reading, solution was really easy.
AVOID using System.Data.SqlClient, use Microsoft.Data.SqlClient package instead. It's able to handle values mentioned in question.

huangapple
  • 本文由 发表于 2023年8月9日 16:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865859-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定