1

我正在尝试使用 VB.Net 前端和Access 2003后端为自己创建一个简单的票务软件。我允许添加新行,但是当我尝试对 Update row 进行编码时button,它给了我一个错误,说Microsoft JET Database Engine - Syntax error in UPDATE statement.我找不到问题所在。

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim ConnectString As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\aaron-pister\Desktop\New Role Notes\Issue Tracker Express\Issue Tracker Express\Issue Tracker.mdb")
    Dim con As New OleDbConnection(ConnectString)
    con.Open()
    Dim Green42 As String = "UPDATE Issues Basic Details SET [Company Name] = '" & txtClientName.Text & "', [Status] = '" & cbStatus.Text & "', [Company Contact] = '" & txtClientContact.Text & "', [Description] = '" & txtDesc.Text & "', [Notes] = '" & txtRes.Text & "' WHERE [TicketNum] = '" & txtTicket.Text & "'"
    'con.Open()
    If txtClientName.Text <> "" Then
        Try
            'MyCom.CommandText = "UPDATE [Issues Basic Details] SET Company Name = '" & txtClientName.Text & "', Status = '" & cbStatus.Text & "', Company Contact = '" & txtClientContact.Text & "', Description = '" & txtDesc.Text & "', Notes = '" & txtRes.Text & "' WHERE TicketNum = '" & txtTicket.Text & "')"
            da = New OleDbDataAdapter(Green42.ToString, ConnectString)
            da.Fill(dt)
            da.Update(EsInfo1, "Issues Basic Details")
            MsgBox("Your record has been updated successfully.", MsgBoxStyle.DefaultButton1, "New Ticket Submitted")
        Catch ex As Exception
            MsgBox(ex.Source & "-" & ex.Message)
            con.Close()
            Exit Sub
        End Try
    Else
        MsgBox("You must have an entry in the Client Name, Client Contact and Status fields. It is recommended to also have a value in the Description field.", MsgBoxStyle.OkOnly, "Issue Tracker Express")
        btnNewIncident_Click(sender, e)
        Exit Sub
    End If
End Sub
4

3 回答 3

4

您的表名也必须用括号括起来:

Dim Green42 As String = "UPDATE [Issues Basic Details] SET [Company Name]..."

此外,使用参数而不是将值放入字符串中。它避免了 SQL 注入。

于 2013-01-08T19:54:20.510 回答
0

这:

UPDATE Issues Basic Details SET ...

不是有效的 SQL。您需要通过限定您的表名来帮助它:

UPDATE [Issues Basic Details] SET ...

其他一些建议:

  1. 使用准备好的语句(参数化你的 SQL 以避免 SQL 注入攻击)
  2. 不要在单击事件处理程序中定义这种类型的行为——有一个帮助类来完成这项工作,这样它就可以被重用并且不直接耦合到 UI。
  3. 使用Using语句。你的OleDbConnection班级实现了IDisposable. 您没有正确处理此连接。
于 2013-01-08T19:54:51.220 回答
0

虽然目前很难阅读您的代码,但看起来您确实正在尝试执行“AdHoc”查询,这可能会导致很多问题。

我建议首先将您的语句更改为参数化查询,以帮助诊断问题。

于 2013-01-08T19:55:14.643 回答