【问题标题】:Error message: Syntax error in UPDATE statement [closed]错误消息:UPDATE 语句中的语法错误 [关闭]
【发布时间】:2015-07-13 13:44:15
【问题描述】:

我不知道为什么每次点击更新按钮时都会出错

UPDATE 语句中的语法错误

我不知道我的代码出了什么问题

这是我的代码:

Public Class Form1
    Private Function vld(ByVal ParamArray ctl() As Object) As Boolean
        For i As Integer = 0 To UBound(ctl)
            If ctl(i).text = "" Then
                ErrorProvider1.SetError(ctl(i), ctl(i).tag)
                Return False
                Exit For
            End If
        Next
        Return True
    End Function
    Dim cn As New OleDbConnection
    Dim cm As New OleDbCommand
    Dim da As OleDbDataAdapter
    Dim dt As New DataTable

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        cn.Close()
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TxtExamtime.Format = DateTimePickerFormat.Custom
        TxtExamtime.CustomFormat = "hh:MM tt"
        cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
        cn.Open()
        FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")

    End Sub

    Private Sub FillDataGridView(ByVal Query As String)
        da = New OleDbDataAdapter(Query, cn)
        dt.Clear()
        da.Fill(dt)
        With DataGridView1
            .DataSource = dt
            .Columns(0).HeaderText = "ID"
            .Columns(1).HeaderText = "Family Name"
            .Columns(2).HeaderText = "Given Name"
            .Columns(3).HeaderText = "Gender"
            .Columns(4).HeaderText = "DOB"
            .Columns(5).HeaderText = "Exam Date"
            .Columns(6).HeaderText = "Exam Time"
            .Columns(7).HeaderText = "Street Name"
            .Columns(8).HeaderText = "House Nr"
            .Columns(9).HeaderText = "PLZ"
            .Columns(10).HeaderText = "City"
            .Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End With
    End Sub

    Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
        Try
            FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'")
            TxtFamilyname.Text = dt.Rows(0).Item(1)
            TxtGivenname.Text = dt.Rows(0).Item(2)
            TxtGender.Text = dt.Rows(0).Item(3)
            TxtDob.Text = dt.Rows(0).Item(4)
            TxtExamdate.Text = dt.Rows(0).Item(5)
            TxtExamtime.Text = dt.Rows(0).Item(6)
            TxtStreet.Text = dt.Rows(0).Item(7)
            TxtHouse.Text = dt.Rows(0).Item(8)
            TxtPlz.Text = dt.Rows(0).Item(9)
            TxtCity.Text = dt.Rows(0).Item(10)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
        End Try
    End Sub

    Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
        If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then
            Exit Sub
        Else

        End If

        Try
            With cm
                .Connection = cn
                .CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
                .ExecuteNonQuery()
            End With
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
            Return
        End Try
        MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
    End Sub

    Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
        Try
            With cm
                .Connection = cn
                .CommandText = "Update from [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQuery()
            End With
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)
            Return
        End Try
        MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
    End Sub

    Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
        Close()
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TxtId.Clear()
        TxtFamilyname.Clear()
        TxtGivenname.Clear()
        TxtStreet.Clear()
        TxtHouse.Clear()
        TxtPlz.Clear()
        TxtCity.Clear()
        'To see all the data in DataGridView
        FillDataGridView("select * from[edit$]")

    End Sub

    Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
        Try
            With cm
                .Connection = cn
                .CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and ID ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQuery()
            End With
            MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)

        End Try
    End Sub
End Class

【问题讨论】:

  • 您应该注意消息告诉您的内容并检查update 查询的语法。 UPDATE FROM... 语法无效。
  • 哪里在所有代码中发生错误?
  • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入

标签: vb.net


【解决方案1】:

Update 语句中没有“From”,因此它应该以...开头

    Update [Edit$]

还有其他一些问题。如果 ID 是一个数字,那么它可能不需要单引号,因为它们也不会停止它......

    Where ID = " & txtId.text & "

嵌入了空格的列名需要括号...

    and [Given Name] = '" & txtGivenName.text & "'

最后,此语句对 SQL 注入开放,在这种情况下,有人可能通过在您的文本框中输入 SQL 对您的表造成严重破坏。请考虑改用参数。

您还应该考虑使用 Microsoft.ACE.OLEDB.12.0,因为您现在使用的那个已经很旧了。

如果您想将 Excel 用作数据库,您可能应该在扩展属性中包含其他参数,特别是您需要 HDR=Yes...

    Extended Properties=""Excel 8.0;HDR=Yes"""

这告诉 OLEDB 您的工作表的第一行包含列名,否则它将使用 F1...Fn(我认为但可能是 C1...Cn)

【讨论】:

  • 我正在使用 Excel 表格来保存我的数据
  • 是的。我错过了。我想,ID 周围的良好边界引号不会造成伤害。我很想亲自尝试一下,但我这里没有任何东西可以尝试。
  • 仍然显示错误为“查询表达式中的语法错误(缺少运算符)”。不知道我的代码有什么问题
  • Izaz,如果不发布您的新代码,这有点难以判断。如果你这样做了,你可以在你填写完 CommandText 并发布它的值之后在调试中暂停执行。
  • 我无法在评论框中发布它太长无法发布我的新代码
【解决方案2】:

除了代码的一些其他问题(例如,您几乎应该总是使用参数化查询)之外,Update from [edit$] set... 是错误的。

只需使用Update [edit$] set...

【讨论】:

    【解决方案3】:

    您的更新语句语法显然不正确:

    请查看以下代码:

     Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
            Try
                With cm
                    .Connection = cn
                    .CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]='" & TxtExamdate.Text & "'and [Exam Time]= '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr]= '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                    .ExecuteNonQuery()
                End With
                FillDataGridView("select * from [edit$]")
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Information, Text)
                Return
            End Try
            MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
        End Sub
    

    将所有列名放在 [ ] 内,否则查询将被错误解释,因为它可能会在遇到空格时立即忽略查询的其余部分

    你已经正确地完成了姓氏,但肯定忽略了它们之间有空格的其他列名

    【讨论】:

    • 现在它显示一条错误消息为“查询表达式中的语法错误(缺少运算符)”
    • @Izaz..正如 Ciaran 所提到的,您的代码几乎没有其他问题。放置所有在它们之间有空格的列名称。例如:考试时间,房子号码等。
    • 仍然显示错误为“查询表达式中的语法错误(缺少运算符)”。不知道我的代码有什么问题
    • 您在分号和andword 之间遗漏了一些空格,例如:...TxtGender.Text & "'and DOB...。应该是...TxtGender.Text & "' and DOB...
    猜你喜欢
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多