与DateTime和空值处理 [英] Dealing with DateTime and Null values

查看:150
本文介绍了与DateTime和空值处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 在我有变量调用DateTime类型modifieddate,它可以设置为null。
  2. 在我填写变量使用DataReader并将其值设置为Nothing如果读者是空
  3. 当我再往下使用变量,在存储过程的抱怨,我不提供价值。 过程或函数'tHistory_Insert'需要参数'@modifieddate',但未提供该

问:如何传递null值到存储过程的任何想法时,空的日期

第1步

 公共modifieddate为可为空(中的DateTime)
 

第2步

 如果IsDBNull以便(DR(modifieddate))= false,那么

     modifieddate = DateTime.Parse(DR(modifieddate))
其他

     modifieddate =无
结束如果
 

第3步

  command.Parameters.Add(@ modifieddate,SqlDbType.DateTime)。价值= modifieddate
command.ExecuteNonQuery()
 

解决方案

如果这没有什么,我认为你必须要通过的DBNull.Value。事情是这样的:

 如果modifieddate是没有那么
  command.Parameters.Add(...)。值=的DBNull.Value
其他
  command.Parameters.Add(...)。值= modifieddate
结束如果
 

  1. I have variable called modifieddate of type DateTime, which can be set to null.
  2. I fill in the variable using a datareader and set the value to nothing if the reader is empty
  3. when I use the variable further down, the store procedure complaints that I am not providing the value. "Procedure or function 'tHistory_Insert' expects parameter '@modifieddate', which was not supplied"

Question: Any ideas on how to pass null values into the store procedure when the date in empty?

Step 1

Public modifieddate As Nullable(Of DateTime)

Step 2

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

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

     modifieddate = Nothing
End If

Step 3

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

解决方案

If it's nothing, I think you have to pass the DBNull.Value. Something like this:

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

这篇关于与DateTime和空值处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆