vb.net - 处理日期时间和空值

标签 vb.net ado.net

  1. 我有一个 DateTime 类型的名为 moddeddate 的变量,可以将其设置为 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()

最佳答案

如果没什么,我想你必须传递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/

相关文章:

jquery - 为什么 MVC3 不绑定(bind)这个(JSON)

VB.Net 密码哈希实践

vb.net - PDF页面计数器

c# - 替换列中的多个字符串元素

entity-framework - EF4 创建自定义 DbFactoryProvider 和其他 Db* 类

.net - Entity Framework 4 : Eager Loading (Include) with filters using Self Tracking Entities

vb.net - 将音频文件从 Windows 窗体应用程序资源复制到硬盘驱动器

c# - 参数化查询

azure - 如何处理使用链接资源托管在 Azure 中的 Web 项目的 web.config 中的连接字符串

c# - WinForms 中的 EF : how to filter data in BindingSource/DGW (. Local.ToBindingList())