简体   繁体   中英

ExecuteNonQuery() from c# throwing exception - arithmetic overflow error converting numeric to data type numeric

I have a weird situation, I have a SP being called from my C# application through ADO.Net, the SP doesn't return any data. It is throwing the below exception, but when i execute the SP with the same parameters in SSMS it runs with out any issues.

There is no other actions apart from calling the SP in my C# code.

Error: System.Data.SqlClient.SqlException (0x80131904): Arithmetic overflow error for type int, value = 2344000000.000000

IN C# code the SP is called using ExecuteNonQuery() and it throws Arithematic overflow exception.

I changed it to ExecuteScalar() and there is no error, which i could confirm that the issue is with either sql server or ADO.Net as SP execution in SSMS always executed without any issues.

As the SP doesn't return any data, ADO.NET ExecuteNonQuery will get the number of rows affected as return value and may be this number is bigger than Power(2,31) and could be the reason of Arithematic overflow exception but i don't have more than Power(2,31) rows in my DB tables. This is just my guess.

This means the value is too big for int type so it overflows

the max value for signed int is 2,147,483,647

so 2344000000 will return this error

The issue, I'm guessing, is that 2344000000 is the number of rows affected. ExecuteNonQuery() returns that as an int , whereas ExecuteScalar() can return that as a long (boxed as an object ) so no overflow happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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