如果在VB.Net中,如果日期的值为NULL则如何传递

时间:2017-10-02 08:23:14

标签: vb.net date nullable

为什么会发生这种情况,保存空日期后,日期变为01/01/1900

预期:如果日期字段中没有任何数据,那么它在数据库中应为null。

下面是我的代码。请帮我修理一下。提前致谢

.AddWithValue("@issuedDate", FormatDate(psIssuedDate.Trim, 
                BTW_Constant.kiDateTimeFormat_01))

2 个答案:

答案 0 :(得分:1)

Dim iDate as Nullable(Of Date) = psIssuedDate.Trim
Dim DateStr as String   ' output Date string

If IsDbNull(iDate) Then
   DateStr = "NULL"
Else
   DateStr = FormatDate(iDate, BTW_Constant.kiDateTimeFormat_01))
End If

.AddWithValue("@issuedDate", DateStr)

答案 1 :(得分:0)

我只是在我的SQL语句中更改,并且它运行良好。 SQL语句如下:

INSERT INTO ClientBankGuarantee ([issuedDate]) VALUES (CAST(NULLIF (@issuedDate, '') AS DATETIME))

对于后面的代码如下:

 If .Item("IssuedDate") Is DBNull.Value Then
         txtIssuedDate.Text = String.Empty
 Else
         txtIssuedDate.Text = CType(FormatString(.Item("IssuedDate")).Trim, Date)
 End If

再次感谢那些与我分享大量信息和提示。