本文介绍了使用查询VB.NET以毫秒为单位输入DateTimePicker的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Data.dll中发生了类型为'System.Data.OleDb.OleDbException'的未处理异常

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

其他信息:查询表达式'7/27/2016 6:20:48 PM'中的语法错误(缺少运算符).

Additional information: Syntax error (missing operator) in query expression '7/27/2016 6:20:48 PM'.

每次我在程序上单击保存按钮时,都会收到此消息.

I got this message every time I click the save button on my program.

这是代码:

Public Sub savetoDB()
    Dim mydate As DateTime
    mydate = Me.dtpDateDel.Value

    con.Open()
    Dim sqlQry As String = "INSERT INTO [tbl_Monitoring] ([Truck Plate No], [Driver], [Helper], [Date of Delivery], [Product], [Payment], [Customer]) VALUES (@p1, @p2, @p3, " & mydate & ", @p5, @p6, @p7)"
    Using cmd As New OleDbCommand(sqlQry, con)
        cmd.Parameters.AddWithValue("@p1", cbxTruck.Text)
        cmd.Parameters.AddWithValue("@p2", cbxDriver.Text)
        cmd.Parameters.AddWithValue("@p3", cbxHelper.Text)
        cmd.Parameters.AddWithValue("@p5", cbxProduct.Text)
        cmd.Parameters.AddWithValue("@p6", txtPayment.Text)
        cmd.Parameters.AddWithValue("@p7", txtCustomer.Text)
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("Save Successfully!")
    End Using

End Sub

错误已引发给cmd.ExecuteNonQuery()

the error was thrown to cmd.ExecuteNonQuery()

推荐答案

那么当您这样做时会发生什么?

So what happens when you do this?

Public Sub savetoDB()
Dim mydate As DateTime
    mydate = Me.dtpDateDel.Value

    con.Open()
    Dim sqlQry As String = "INSERT INTO [tbl_Monitoring] ([Truck Plate No], [Driver], [Helper], [Date of Delivery], [Product], [Payment], [Customer]) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7)"
    Using cmd As New OleDbCommand(sqlQry, con)
        cmd.Parameters.AddWithValue("@p1", cbxTruck.Text)
        cmd.Parameters.AddWithValue("@p2", cbxDriver.Text)
        cmd.Parameters.AddWithValue("@p3", cbxHelper.Text)
        cmd.Parameters.AddWithValue("@p4", mydate)
        // Alternatively you need to use something like this to format it correctly.
        //cmd.Parameters.AddWithValue("@p4", mydate.ToString("dd/mm/yyyy hh:mm"))
        cmd.Parameters.AddWithValue("@p5", cbxProduct.Text)
        cmd.Parameters.AddWithValue("@p6", txtPayment.Text)
        cmd.Parameters.AddWithValue("@p7", txtCustomer.Text)
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("Save Successfully!")
    End Using
End Sub

这篇关于使用查询VB.NET以毫秒为单位输入DateTimePicker的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:23