英文:
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["ColName"];
The value can easily be parsed to decimal using eg. decimal.Parse("888512650000.0")
, 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("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"]; //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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论