从字符串到日期类型的转换无效

conversion from string to type date is not valid

在这件事上我真的需要你的帮助。我搜索了许多与此相关的类似主题,但找不到适合我的程序的主题。我会尽量简化我的问题并排除与我的问题无关的其他细节。

我的 vb 程序有一个名为 dtpDate 的日期时间选择器、两个名为 btnRecord_Save 和 btnClear_Date 的按钮。

我的 sql 有一个名为 table 的凭证和一个名为 Voucher_Date 的 table(数据类型 = 日期)

当我的 dtpdate 有一个日期值时,我没有问题,因为它工作得很好,但是如果我通过按 btnClear_date 清除 dtpdate 上的日期,它会给我一个错误(如上标题所述).我想要的只是如果 dtpDate 为空,它将在我的 sql 上存储 'NULL' 值(列名 = Voucher_date)。

这是我的代码供您参考,我已经编辑并删除了不必要的信息。

CLASS 代码 (SQLControl)

Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class SQLControl

 Public SQLCon As New SqlConnection With {.ConnectionString i got this!!!}
 Public SQLDA As SqlDataAdapter
 Public SQLDS As DataSet
 Public Params As New List(Of SqlParameter)
 Public RecordCount As Integer
 Public Exception As String
 Private SQLCmd As SqlCommand
 Public Sub AddToVoucher(Voucher_Date As Date)
    Try
        Dim strInsert As String = "INSERT INTO Voucher (Voucher_Date) " & _
                                  "VALUES (" & _
                                  "'" & Voucher_Date & "') "
        SQLCon.Open()
        SQLCmd = New SqlCommand(strInsert, SQLCon)
        SQLCmd.ExecuteNonQuery()
        SQLCon.Close()
        MsgBox("Successfully Added Into Records!")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
 End Sub
End Class


Public Class FormMain

 Dim sql As New SQLControl

 Private Sub dtpDate_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtpDate.ValueChanged
    dtpDate.CustomFormat = "dd/MM/yyyy"
 End Sub

 Private Sub btnClearDate_Click(sender As System.Object, e As System.EventArgs) Handles btnClearDate.Click
    dtpDate.CustomFormat = "        "
    dtpDate.Format = DateTimePickerFormat.Custom
 End Sub

 Private Sub btnRecord_Save_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord_Save.Click
        sql.AddToVoucher(dtpDate.Text)
 End Sub

End Class

我刚刚做了一些实验,确实 DateTimePicker(使用您的代码)

dtpDate.CustomFormat = "        "
dtpDate.Format = DateTimePickerFormat.Custom

仍将保留该值。但是它在Text 属性中会有一串空格。所以这就是您的代码发生的情况:

  • 当你有日期的字符串表示时,它会在参数 Voucher_Date As Date

  • 中隐式转换为 Date
  • 当您有 " " - 它无法转换为 Date,因此出现错误。

现在,我不会告诉你你代码的所有错误。我将指出如何退出您在 您的 自己的编码方式中遇到的情况。不幸的是,你最好走参数化路线,这样更容易。

在您的 class 中为自定义格式声明常量

Private const _cust_Format AS String = "        "

然后这样做

Private Sub btnClearDate_Click(sender As System.Object, e As System.EventArgs) Handles btnClearDate.Click
    dtpDate.CustomFormat = _cust_Format ' <-- so, you always use same number of spaces
    dtpDate.Format = DateTimePickerFormat.Custom
End Sub

Public Sub AddToVoucher(Voucher_Date As DateTime?)
    Try
        Dim strInsert As String = "INSERT INTO Voucher (Voucher_Date) VALUES (@1)"

        SQLCon.Open()
        SQLCmd = New SqlCommand(strInsert, SQLCon)
        ' New code:
        SQLCmd.Parameters.AddWithValue("@1", If(Voucher_Date Is Nothing, DbNull.Value, Voucher_Date)) 
        ' Interestingly, Sql Client doesn't like to set Nothing/Null to value, it likes DBNull.Value. ODP likes both. hmmm...
        SQLCmd.ExecuteNonQuery()
        SQLCon.Close()
        MsgBox("Successfully Added Into Records!")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub 

Private Sub btnRecord_Save_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord_Save.Click
    ' new code:
    Dim d As DateTime? = Nothing
    If dtpDate.Text <> _cust_Format Then
        d = dtpDate.Value   '<-- notice this
    End If  
    sql.AddToVoucher(d)
End Sub

我还没有测试过它,但这应该能让你的代码暂时工作。