为什么在 ADO.NET 中将数字转换为数字数据类型时会出现算术溢出错误?

发布于 2024-08-14 12:14:32 字数 681 浏览 1 评论 0原文

原始问题:

当金额大于 $999,999,99 时,为什么在使用 Money 数据类型的 ADO.NET 代码中将数字转换为数字数据类型时会出现算术溢出错误?


正如问题所说。我在数据访问层中有一些与 Sql Server 2008 数据库通信的 ADO.net 代码。表中有一个金额列,其数据类型为“Money”。当插入金额 < 的记录时,代码可以正常工作。 $1,000,000 但当金额 >= $1,000,000 时抛出此错误:

“将数字转换为数据类型数字时出现算术溢出错误”

我可以针对数据库手动运行 t-sql,将金额更新为大于 $1,000,000 的值,以便数据库可以排除该金额很好...下面的 SqlCommand 是什么导致错误发生?

        MyCommand.Parameters.Add(New SqlParameter("@Amount", SqlDbType.Money))
        If IsNothing(Amount) Then
            MyCommand.Parameters("@Amount").Value = Convert.DBNull
        Else
            MyCommand.Parameters("@Amount").Value = Amount
        End If

Original Question:

Why do I get an Arithmetic overflow error converting numeric to data type numeric in ADO.NET code using the Money Data Type when amount is greater than $999,999,99?


Just as the question says... I have a bit of ADO.net code in the data access layer that talks to a Sql Server 2008 database. There is an Amount column in the table that is of data type "Money". The code works fine when inserting a record with an amount < $1,000,000 but throws this error when amount is >= $1,000,000:

"Arithmetic overflow error converting numeric to data type numeric"

I can manually run t-sql against the database updating the amount to a value larger than $1,000,000 so the database can except the amount fine... what is it about the following SqlCommand that causes the error to fire?

        MyCommand.Parameters.Add(New SqlParameter("@Amount", SqlDbType.Money))
        If IsNothing(Amount) Then
            MyCommand.Parameters("@Amount").Value = Convert.DBNull
        Else
            MyCommand.Parameters("@Amount").Value = Amount
        End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-08-21 12:14:32

如果使用某个值调用存储过程时发生错误,但使用该值直接更新表时没有发生错误,那么就会对 @Amount 参数的数据类型产生怀疑。确保它也被定义为金钱。

If an error occurs calling a stored procedure with a value, but no error occurs when directly updating the table with that same value, then that casts suspition upon the data type of the @Amount parameter. Make sure it is also defined as Money.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文