使日期为空

有人可以帮助我修复我的代码,我试图通过一个函数,除了_dtSWO在SQL服务器中的空值。

如果我设置_dtSWO = Nothing,它会返回#12:00:00 AM#,这会引发异常。 该字段在SQL Server中可以为空,我只是希望它返回一个空格

Dim _swoDate As String = udSWOReceivedDateDateEdit.Text
        Dim _dtSWO As Date

        If _swoDate <> "" Then
            _dtSWO = Date.Parse(udSWOReceivedDateDateEdit.Text)
        Else
            'THIS DOESNT WORK
            _dtSWO = Nothing
        End If

您需要使用可空类型。 默认情况下,Date不会将null(VB.Net中的Nothing)作为值。 可空类型接受null(Nothing)。

    Dim _dtSWO As Nullable(Of Date)

    If String.IsNullOrEmpty(_swoDate) Then
        _dtSWO = Date.Parse(udSWOReceivedDateDateEdit.Text)
    Else
        _dtSWO = Nothing
    End If

根据评论编辑。


使其可空。 与Nullable Integer问题相同的情况。

Dim _swoDate As String = udSWOReceivedDateDateEdit.Text
Dim _dtSWO As Date?

If _swoDate <> "" Then
    _dtSWO = Date.Parse(udSWOReceivedDateDateEdit.Text)
Else
    _dtSWO = Nothing
End If
链接地址: http://www.djcxy.com/p/42823.html

上一篇: Make A Date Null

下一篇: Can my users inject my dynamic sql?