C# 无法将DBNull与NULL列值匹配

C# 无法将DBNull与NULL列值匹配,c#,.net,sql-server,entity-framework,ado.net,C#,.net,Sql Server,Entity Framework,Ado.net,我有一个web应用程序,其中设置了一个参数,如果值为null,则将存储过程调用为DBNull,如下所示- new SqlParameter(Utilities.Constants.SpParameters.EffectiveDate,r["EffectiveDate"] == null? DBNull.Value:r["EffectiveDate"]), 其中r[“EffectiveDate”]是数据表列“EffectiveDate”的行中的值。 现在,

我有一个web应用程序,其中设置了一个参数,如果值为null,则将存储过程调用为DBNull,如下所示-

new SqlParameter(Utilities.Constants.SpParameters.EffectiveDate,r["EffectiveDate"] == null? DBNull.Value:r["EffectiveDate"]),
其中r[“EffectiveDate”]是数据表列“EffectiveDate”的行中的值。 现在,在存储过程中,我检查表中是否存在一条记录,该记录的值由参数提供。如果是,则更新记录,否则将其插入

表中有一条记录的EffectiveDate为NULL,但当我尝试使用IF EXISTS条件检查和更新它时,它在尝试更新时找不到与具有DBNull值的条目匹配的项,并继续插入一条新记录

我尝试了下面的选项,但没有效果-

1. CAST(EffectiveDate as datetime) = CAST(@EffectiveDate as Datetime) AND 

2. ISNULL (EffectiveDate ,'1900-01-01 00:00:00.000') = CASE 
WHEN @EffectiveDate IS NULL THEN '1900-01-01 00:00:00.000'
WHEN ISDATE(@EffectiveDate) = 0 THEN '1900-01-01 00:00:00.000'
WHEN CAST(@EffectiveDate as datetime) = '1900-01-01 00:00:00.000' THEN '1900-01-01 00:00:00.000'
ELSE @EffectiveDate END

3. SET @EffectiveDate = NULLIF(@EffectiveDate, '')  before the if condition
完整的存储过程如下所示-

USE [Process1]
GO
/****** Object:  StoredProcedure [dbo]. 
[sp_Insert_Update_History]    Script Date: 4/19/2021 7:55:12 PM 
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_Insert_Update_History] 
-- Add the parameters for the stored procedure here
    @CompanyName                VARCHAR(500),
    @Bank                       VARCHAR(100) NULL,      
    @EffectiveDate      DATETIME NULL,
    @ExpirationDate     DATETIME NULL,
    @Amount                 DECIMAL(18,2) NULL,
    @Adjustment         DECIMAL(18,2) NULL,     
    @Rec_Insert_Update_By       BIGINT,
    @Rec_Insert_Update_Flag     CHAR(1)
AS
BEGIN

BEGIN TRY
--SET @EffectiveDate = NULLIF(@EffectiveDate, '')

IF EXISTS (SELECT 1 FROM dbo.LOC_HISTORY WHERE 
                CompanyName = @CompanyName AND              
                    
                CAST(EffectiveDate as datetime) = CAST(@EffectiveDate as 
Datetime) AND 
                --ISNULL (EffectiveDate ,'1900-01-01 00:00:00.000') = CASE 
                --WHEN @EffectiveDate IS NULL THEN '1900-01-01 00:00:00.000'
                --WHEN ISDATE(@EffectiveDate) = 0 THEN '1900-01-01 
00:00:00.000'
                --WHEN CAST(@EffectiveDate as datetime) = '1900-01-01 
00:00:00.000' THEN '1900-01-01 00:00:00.000'
                --ELSE @EffectiveDate END AND

            
                CAST(ExpirationDate as datetime) = CAST(@ExpirationDate as 
datetime)
                )

    BEGIN
        UPDATE dbo.LOC_HISTORY SET
        
        CompanyName                     =  @CompanyName        
        ,Bank                       =  @Bank  
        ,EffectiveDate              =  @EffectiveDate          
        ,ExpirationDate             =  @ExpirationDate                 
        ,Amount                         =  @Amount         
        ,Adjustment                 =  @Adjustment         
        
        ,Rec_Insert_Update_Dt               =  GETDATE()                   
        ,Rec_Insert_Update_By               =  @Rec_Insert_Update_By                   
        ,Rec_Insert_Update_Flag             =  'U'

        WHERE           
        CompanyName = @CompanyName AND
                        
                EffectiveDate = @EffectiveDate AND
                ExpirationDate = @ExpirationDate        

    END
 IF NOT EXISTS (SELECT 1 FROM dbo.LOC_HISTORY WHERE 
                CompanyName = @CompanyName AND                                      
                EffectiveDate = @EffectiveDate AND
                ExpirationDate = @ExpirationDate 
                )
    BEGIN
        INSERT INTO dbo.LOC_HISTORY(
          [CompanyName]
          ,[Bank]             
          ,[EffectiveDate]
          ,[ExpirationDate]
          ,[Amount]
          ,[Adjustment]           
          ,[Rec_Insert_Update_Dt]
          ,[Rec_Insert_Update_By]
          ,[Rec_Insert_Update_Flag]
          )
        
        VALUES ( 
        @CompanyName
          ,@Bank              
          ,@EffectiveDate
          ,@ExpirationDate
          ,@Amount
          ,@Adjustment                             
        , GETDATE()                
        ,@Rec_Insert_Update_By                 
        ,'I')       

                

    END 
END TRY

BEGIN CATCH

SELECT ERROR_MESSAGE()
END CATCH
END

我缺少什么?

从数据库返回的是DBNull.Value。所以我想你需要在双等号“==DBNull.Value”之后,在这种情况下,不需要翻译。你提到的
存在于哪里?显示整个过程,而不是没有上下文的片段。另外,声明参数并将其作为实际日期/时间类型传递,可以消除所有容易出错的讨厌的字符串工作。将
NULL
与参数匹配的正确方法是
(@EffectiveDate为NULL,EffectiveDate为NULL)或@EffectiveDate=EffectiveDate
。您无法将
NULL
NULL
进行比较,因为
NULL
从不与任何东西进行比较(它是“未知的”,因此结果也是“未知的”)。因此,您必须进行单独的
IS NULL
检查。永远不要操纵字符串来比较日期。这能回答你的问题吗?