我的代码中的Update语句中的语法错误

时间:2014-02-25 02:21:33

标签: vb.net

美好的一天,我有以下代码:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim cmd As New OleDb.OleDbCommand
    Dim compDate As Date
    Dim x As New Integer
    Dim profID As New Integer
    Dim date1 As New Date
    compDate = Format(Date.Now, "hh:mm:ss, tt")
    'MsgBox(compDate)
    date1 = #8:00:00 AM#
    profID = 201400001
    x = 1
    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If
    cmd.Connection = cnn
    'Timer1.Start()
    Timer1.Interval = 5000
    sp.Close()
    Try
        sp.Open()
    Catch
        sp.Close()
    End Try
    If TextBox1.Text = "201400001" Then
        If DateDiff(DateInterval.Minute, date1, Date.Now) > 5 Then
            MsgBox("been here")
            cmd.CommandText = "UPDATE test " & _
                              "SET ProfLog" & x & "" & _
                              "WHERE ProfID='" & Me.TextBox1.Text & "' AND ProfTime=#" & date1 & "#"
            cmd.ExecuteNonQuery()
            MsgBox("Did this")
        End If
        MsgBox("Done!")
    ElseIf TextBox1.Text = "201400002" Then
        MsgBox("Hello World Again!")
    ElseIf TextBox1.Text = "201400003" Then
        MsgBox("My Turn!")
    End If
    TextBox1.Clear()
End Sub

一旦到达cmd.ExecuteNonQuery,就会显示语法错误。它说在UPDATE语句中有一个“语法错误”我想知道什么是使我的程序出错的语法。提前致谢。

1 个答案:

答案 0 :(得分:2)

您的UPDATE语句中有错误。您在=之后遗漏了SET ProfLog

"UPDATE test " & _
    "SET ProfLog = " & x & "" & _
    "WHERE ProfID='" & Me.TextBox1.Text & "' AND ProfTime=#" & date1 & "#"

您可以通过在消息框中显示cmd.CommandText或在Visual Studio中显示即时窗口来自己解决这个问题。

请自己巨大的青睐并搜索“参数化查询”或“SQL注入”。你应该从一开始就学会正确地做事,而不是学会做得不好,以后会给自己造成很多很多问题。