gpt4 book ai didi

c# - 如何为 dbtype decimal 传递空值?

转载 作者:行者123 更新时间:2023-11-30 13:40:12 32 4
gpt4 key购买 nike

我将参数传递给存储过程:

param = CreateInParameter("Result", DbType.Decimal);
param.Value = testresults.Result;
cmd.Parameters.Add(param);

有时 testresults.Result 会是 null

如何将其作为 NULL 传递?

这是我的存储过程:

USE [SalesDWH]
GO
/****** Object: StoredProcedure [dbo].[Insert_TestResults] Script Date: 12/25/2011 23:49:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Insert_TestResults]
-- Add the parameters for the stored procedure here

@TestName varchar (500),
@Result decimal (18,4)=null,
@NonNumericResult varchar (50)=null,
@QuickLabDumpid int

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

INSERT INTO [SalesDWH].[dbo].[TestResults]
([TestName]
,[Result]
,nonnumericresult
,[QuickLabDumpid])
VALUES
(@TestName,@Result,@nonnumericresult,@QuickLabDumpID)


END

如果 testresults.Result 没有被赋值,它只是作为 0 进入。我如何允许它作为 NULL 进入数据库?

最佳答案

试试这个:

param.Value = testresults.Result==0?DBNull.Value:(object)testresults.Result;

关于c# - 如何为 dbtype decimal 传递空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8633921/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com