【问题标题】:Set Datarow column of SQLDateTime to Null将 SQLDateTime 的 Datarow 列设置为 Null
【发布时间】:2017-08-09 22:22:51
【问题描述】:

我正在加载一些 ADO.net 对象:

    Dim conPERP As New System.Data.SqlClient.SqlConnection(OISConnectString)
    Dim cmd As New System.Data.SqlClient.SqlCommand("", conPERP)
    daCERTs.SelectCommand = New SqlCommand("SELECT * FROM PERP_Certs_DMS", conPERP)
    Dim cmdBldr As New SqlCommandBuilder(daCERTs) 
    daCERTs.Fill(dsCERTs)
    daCERTs.FillSchema(dsCERTs, System.Data.SchemaType.Mapped) ' later troubleshooting attempt
    dtCERTs = dsCERTs.Tables(0)

使用 SQL Server 2008。dtCERTs 中的一列是 SQL 类型的日期时间。该列可以为空。报告已经有一个值,但我需要将其设置回 NULL。

        sWhere = $"registrationNo={c("regNo")} AND Instance_ID={sInst}"
        row = dtCERTs.Select(sWhere)(0)
        row("Why_Archived") = DBNull.Value ' varchar - works
        row("Record_Archive_Date") = SqlDateTime.Null ' causes error

最后一行导致错误:

Unable to cast object of type 'System.Data.SqlTypes.SqlDateTime'
to type 'System.IConvertible'.Couldn't store <Null> 
in Record_Archive_Date Column.  Expected type is DateTime.

Null.Value 不起作用。

SqldateTime.Null.Value 导致错误:“数据为 Null。无法对 Null 值调用此方法或属性。”

如何将日期时间列设置为 NULL?

【问题讨论】:

    标签: sql-server vb.net sql-server-2008 ado.net


    【解决方案1】:

    使用

    row("Record_Archive_Date") = DBNull.Value
    

    正如您对varchar 所做的那样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多