2012-08-15 82 views
2
  1. 我有一个名为类型DateTime的修改日期,它可以被设置为null的变量。
  2. 我使用数据读取器填写变量,如果读者为空,则将该值设置为空
  3. 当我进一步使用该变量时,存储过程投诉说我没有提供该值。 “过程或函数'tHistory_Insert'期望参数'@modifieddate',它没有提供”

问题:有关如何将空值传入存储过程时的空值?处理日期时间和空值

步骤1

Public modifieddate As Nullable(Of DateTime) 

步骤2

If IsDBNull(dr("modifieddate")) = False Then 

    modifieddate = DateTime.Parse(dr("modifieddate")) 
Else 

    modifieddate = Nothing 
End If 

步骤3

command.Parameters.Add("@modifieddate", SqlDbType.DateTime).Value = modifieddate 
command.ExecuteNonQuery() 
+1

改变存储过程的参数为'@modifieddate日期时间= null' – 2012-08-15 14:36:34

回答

5

如果这没有什么,我想,如果将t o传递DBNull.Value。事情是这样的:

If modifieddate Is Nothing then 
    command.Parameters.Add(...).Value = DBNull.Value 
Else 
    command.Parameters.Add(...).Value = modifieddate 
End If 
+0

而你并不需要使用可空(日期时间中),您可以只使用日期时间和测试Nothing或使用日期。 MinValue就像你一样。 – 2012-08-15 15:09:19

+0

@rs。我只是试了一下。我的存储过程没有使用'= null'设置参数,我把它传递给'DBNull.Value'。你指的是桌子本身吗? – LarsTech 2012-08-15 15:18:17