简体   繁体   English

将数字转换为数据类型数字时出错

[英]Error when converting numeric to data type numeric

I have little error when converting numeric to data type numeric. 将数字转换为数据类型数字时,我几乎没有错误。 i need to take hours(decimal) between 2 date (dateTime). 我需要花费2个日期(dateTime)之间的小时(十进制)。

but I see error 但我看到错误

System.Data.SqlClient.SqlException: Arithmetic overflow error converting numeric to data type numeric. System.Data.SqlClient.SqlException:将数值转换为数据类型numeric的算术溢出错误。

var hours = (dateTime1 - dateTime2).TotalHours;
res = Math.Round(Convert.ToDecimal(hours),2);

how can i clean take hours? 我怎么能清洁需要几个小时?

  1. Don't convert TotalHours to decimal, it is a double , just leave that as is 不要将TotalHours转换为十进制,它是一个精度,只是保持原样
  2. Ensure that the column in the SQL table you are writing this to is of type float 确保要写入此SQL表的SQL列的类型为float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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