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

时间:2015-01-03 15:49:41

标签: vb.net string date sqldatatypes

我真的需要你的帮助。我已经搜索了很多类似的主题,但找不到适合我程序的主题。我试图简化我的问题,并排除与我的关注无关的其他细节。

我的vb程序有一个名为dtpDate的datetimepicker,名为btnRecord_Save和btnClear_Date的2个按钮。

我的sql有一个名为Voucher的表,其中有一个名为Voucher_Date的表(Datatype = Date)

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

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

CLASS CODE(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

1 个答案:

答案 0 :(得分:0)

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

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

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

  • 如果您拥有日期的字符串表示形式,则会在参数Date中隐式转换为Voucher_Date As Date

  • 当你有" "时,它无法转换为Date,因此会出错。

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

在你的类中声明自定义格式的常量

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

我还没有测试过,但这应该会让你的代码现在正常运行。

相关问题