[英]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.
数据来自WEB API,以下数据列也不例外。
"RRP": 2.98074513,
"Cost": 2.533633360
But when it is trying to add the following data sets it shows an SQL Exception 但是,当尝试添加以下数据集时,它会显示SQL异常
"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" 例外是“ Core .Net SqlClient数据提供程序,MessageArithmetic溢出错误将int转换为数值类型的数据”
SQL Table structure - SQL表结构-
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# - 在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') 插入DailyConsumptionLogs(ResourceId,RRP,成本)值(1,4.90,4.1650)插入DailyConsumptionLogs(ResourceId,RRP,成本)值(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 两者都定义为
numeric(15, 13)
数据类型
Yes insert it directly to the DB gets the following exception in SQL server 是的,将其直接插入数据库中会在SQL Server中获得以下异常
Arithmetic overflow error converting int to data type numeric. 将int转换为数值数据类型的算术溢出错误。 The statement has been terminated.
该语句已终止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.