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

标签 c# sql sql-server

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

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/

相关文章:

c# - Azure blob 文件名中的模式搜索

c# - 为什么 ToolStripMenuItems 会发生这种情况?

c# - 如何强制泛型类使用带参数的 T 构造函数?

mysql - 根据 Hive 中另一个表中的时间戳在表中查找记录

java - Spring、JdbcTemplate不能使用Select List中未指定的Column吗?

sql-server - 使用Redis实现的Sql Server内存表

c# - ASP.NET EF6 - 2 个表和 1 个链接表

c# - Dapper 可以处理可为空的 bool /位字段吗? [无效的]

c# - xslt V1.0 - 具有递归循环的子模板返回空值

php - 如果表中的日期和时间具有不同的字段,如何使用 BETWEEN 日期和时间 mysql