- 我有一个 DateTime 类型的名为 moddeddate 的变量,可以将其设置为 null。
- 我使用数据读取器填充变量,如果读取器为空,则将值设置为空
- 当我进一步使用该变量时,存储过程提示我没有提供该值。 “过程或函数‘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()
最佳答案
如果没什么,我想你必须传递DBNull.Value。像这样的事情:
If modifieddate Is Nothing then
command.Parameters.Add(...).Value = DBNull.Value
Else
command.Parameters.Add(...).Value = modifieddate
End If
关于vb.net - 处理日期时间和空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11971269/