处理DateTime和Null值

Int*_*eer 2 vb.net ado.net

  1. 我有一个名为DateTime类型的modifieddate变量,可以设置为null.
  2. 我使用datareader填充变量,如果读取器为空,则将值设置为空
  3. 当我进一步使用变量时,商店程序抱怨我没有提供价值."过程或函数'tHistory_Insert'需要参数'@modifieddate',这是未提供的"

问题:当日期为空时,有关如何将空值传递到存储过程的任何想法?

步骤1

Public modifieddate As Nullable(Of DateTime)
Run Code Online (Sandbox Code Playgroud)

第2步

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

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

     modifieddate = Nothing
End If
Run Code Online (Sandbox Code Playgroud)

第3步

command.Parameters.Add("@modifieddate", SqlDbType.DateTime).Value = modifieddate
command.ExecuteNonQuery()
Run Code Online (Sandbox Code Playgroud)

Lar*_*ech 5

如果没什么,我认为你必须通过DBNull.Value.像这样的东西:

If modifieddate Is Nothing then
  command.Parameters.Add(...).Value = DBNull.Value
Else
  command.Parameters.Add(...).Value = modifieddate
End If
Run Code Online (Sandbox Code Playgroud)