我有一个包含一个名为Days1的已定义字段的表decimal(5,2)
此表在edmx文件中映射,并且有一个向该表插入值的过程。
每当我尝试使用我的代码
插入值7.5时x.Days1 = 7.5;
我收到以下错误:
“将数据类型数字转换为十进制时出错。”
如果我执行具有相同值的过程,则不会发生错误。 如果我将值7.51与我的代码一起使用:
x.Days1 = 7.51
没有出现错误。我认为问题在于点后面的位数。我需要为值7.5做什么?如何将其发送为7.50
存储过程:
ALTER PROCEDURE [dbo].[proc_EmployeeBalanceInsert]
(
@EmployeeBalanceID char(36),
@EmployeeID char(36),
@LeaveRegulationID char(36),
@AttendanceTypeID char(36),
@BalanceOpenDate datetime,
@DateFrom datetime,
@DateTo datetime,
@BalanceUpToDate datetime = NULL,
@ValidUntilDate datetime,
@LeaveTransactionTypeID char(1) ,
@TransactionSign int = NULL,
@Days1 decimal(5,2) = NULL,
@Days2 decimal(5,2) = NULL,
@Days3 decimal(5,2) = NULL,
@Days4 decimal(5,2) = NULL,
@Days5 decimal(5,2) = NULL,
@System int = NULL,
@CreationDate datetime = NULL,
@BatchID char(36) = NULL
)
AS
BEGIN
SET NUMERIC_ROUNDABORT on
SET NOCOUNT OFF
DECLARE @Err int
INSERT
INTO [EmployeeBalance]
(
[EmployeeBalanceID], [EmployeeID], [LeaveRegulationID],
[AttendanceTypeID], [BalanceOpenDate], [DateFrom],
[DateTo], [BalanceUpToDate], [ValidUntilDate],
[LeaveTransactionTypeID], [TransactionSign], [Days1],
[Days2], [Days3], [Days4],
[Days5], [System], [CreationDate],
[BatchID]
)
VALUES
(
@EmployeeBalanceID, @EmployeeID, @LeaveRegulationID,
@AttendanceTypeID, @BalanceOpenDate, @DateFrom,
@DateTo, @BalanceUpToDate, @ValidUntilDate,
@LeaveTransactionTypeID, @TransactionSign, @Days1,
@Days2, @Days3, @Days4,
@Days5, @System, @CreationDate,
@BatchID
)
SET @Err = @@Error
RETURN @Err
END
答案 0 :(得分:3)
我通过在保存数据之前添加以下代码来解决我的问题:
x.Days1 = Convert.ToDecimal(x.Days1.ToString("n2"));
答案 1 :(得分:1)
尝试将变量设置为“true”十进制值:
x.Days1 = 7.5m; // Notice the "m" appended to the value