英文:
System.OverflowException: 'Conversion overflows.' during reading Decimal from DB
问题
我在数据库中有一个相当大的值:888512650000.0。
我使用SqlBulkCopy
将其存储在数据库中时没有问题。
但是,当我尝试使用SqlCommand
-> SqlDataReader
读取此值时,出现错误:
System.OverflowException: Conversion overflows
即使我使用简单的代码:
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: 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.
$(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论