简体   繁体   中英

Exception while adding data to to the database -overflow error converting int to data type numeric

I'm getting an exception for some data sets when I add them to the table. Data is coming from a WEB API and there are no exceptions for the following data columns.

"RRP": 2.98074513,
"Cost": 2.533633360

But when it is trying to add the following data sets it shows an SQL Exception

"RRP": 2.92,
"Cost": 2.4820

and this -

"RRP": 4.90,
"Cost": 4.1650

The exception is "Core .Net SqlClient Data Provider, MessageArithmetic overflow error converting int to data type numeric"

SQL Table structure -

CREATE TABLE [dbo].[DailyConsumptionLogs](
    [UsageLineId] [int] IDENTITY(1,1) NOT NULL,
    [ResourceId] [varchar](500) NOT NULL,
    [RRP] [numeric](15, 13) NOT NULL,
    [Cost] [numeric](15, 13) NULL
) ON [PRIMARY]

Insert statement executed in C# -

string sql_command = String.Format(@"INSERT INTO DailyConsumptionLogs(
                                                        ResourceId,
                                                        RRP,
                                                        Cost) 
                                  VALUES ('{0}','{1}','{2}')",
                                               item.ResourceId,
                                               item.RRP,
                                               item.Cost);
  1. Could you share the value of the sql_command after the execution of the assignment line? Both correct and incorrect.
  2. How are item.RRP, item.Cost defined?
  3. Do you have any exception inserting directly to the DB?

    INSERT INTO DailyConsumptionLogs(ResourceId, RRP, Cost) VALUES (1,4.90,4.1650) INSERT INTO DailyConsumptionLogs(ResourceId, RRP, Cost) VALUES (1,'2.92', '2.4820')

  1. This is the command taken after using a breakpoint -

Correct command

  INSERT INTO DailyConsumptionLogs(ResourceId,RRP,Cost)VALUES('03E2F28B',2.98074513,2.53363336)

Exception Command

INSERT INTO DailyConsumptionLogs (ResourceId,RRP,Cost)v VALUES ('03E2F28B',4.9,4.165)
  1. Both are defined as numeric(15, 13) data type

  2. Yes insert it directly to the DB gets the following exception in SQL server

Arithmetic overflow error converting int to data type numeric. The statement has been terminated.

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