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);
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')
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)
Both are defined as numeric(15, 13)
data type
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.