【问题标题】:INSERT / UPDATE error updating access database through VB.net通过 VB.net 更新访问数据库的 INSERT / UPDATE 错误
【发布时间】:2018-09-23 19:57:23
【问题描述】:

我正在尝试通过 VB 在 access 数据库文件中更新/保存新记录。

当我运行应用程序并按下保存或更新按钮时,我收到 2 个错误:

附加信息:INSERT INTO 语句中的语法错误。

附加信息:UPDATE 语句中的语法错误。

谁能看出我的语法有问题?

我会附上代码和GUI截图

Imports System.Data.OleDb

Public Class Form1

    Dim dbconn As New OleDbConnection
    Dim adt As New OleDbDataAdapter
    Dim ds As New DataSet

    Dim datatable As New DataTable
    Dim cmd As New OleDbCommand


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dbconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; data source = CUBSDatabase.accdb"
        showData()
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        adt = New OleDbDataAdapter("insert into Student (FName, SName, Attendance, CA1, CA2, FinalExam) values ( '" & txtFName.Text & "','" & txtSName.Text & "',  '" & txtAttendance.Text & "', '" & txtCA1.Text & "', '" & txtCA2.Text & "', '" & txtFinalExam.Text & "', )", dbconn)

        adt.Fill(ds)
        ds = New DataSet
        showData()
        MsgBox("Saved")
    End Sub

    Private Sub showData()
        Dim dbcommand As String
        dbcommand = "SELECT * FROM Student"
        adt = New OleDbDataAdapter(dbcommand, dbconn)
        datatable = New DataTable
        adt.Fill(datatable)
        DataGridView1.DataSource = datatable
    End Sub

    Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
        Dim sql = "select * from Student where ID =" & txtID.Text & " "
        adt = New OleDbDataAdapter(sql, dbconn)
        cmd = New OleDbCommand(sql)
        adt.Fill(ds, "Student")

        txtFName.Text = ds.Tables("Student").Rows(0)(1).ToString
        txtSName.Text = ds.Tables("Student").Rows(0)(2).ToString
        txtAttendance.Text = ds.Tables("Student").Rows(0)(3).ToString
        txtCA1.Text = ds.Tables("Student").Rows(0)(4).ToString
        txtCA2.Text = ds.Tables("Student").Rows(0)(5).ToString
        txtFinalExam.Text = ds.Tables("Student").Rows(0)(6).ToString
        ds = New DataSet
    End Sub

    Private Sub TabPage1_Click(sender As Object, e As EventArgs) Handles TabPage1.Click

    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1'" & txtCA1.Text & "', CA2'" & txtCA2.Text & "', FinalExam'" & txtFinalExam.Text & "'where ID=" & txtID.Text & "", dbconn)

        adt.Fill(ds)
        ds = New DataSet
        showData() ' refresh data in datagridview
        MsgBox("Updated")
    End Sub
End Class

这是我的图形用户界面

【问题讨论】:

  • 您的值列表末尾有一个额外的逗号 "', ) remove this '" & txtFinalExam.Text & "', ) 应该是 '" & txtFinalExam.Text & "')
  • 更新命令中也缺少很多等号
  • 谢谢Sami 这已经解决了更新问题
  • 总是使用参数来避免sql注入和格式化错误。

标签: vb.net vba visual-studio visual-studio-2015


【解决方案1】:

James 就快到了,但我认为这应该可以帮到你...

                adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text _
                                           & "', SName='" & txtSName.Text _
                                           & "', Attendance='" & txtAttendance.Text _
                                           & "', CA1='" & txtCA1.Text _
                                           & "', CA2='" & txtCA2.Text _
                                           & "', FinalExam='" & txtFinalExam.Text _
                                           & "' where ID='" & txtID.Text & "'", dbconn)

为了便于阅读,我已将其屏蔽,如果您愿意,完整的字符串也在下面(两者都做同样的事情)。

adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1='" & txtCA1.Text & "', CA2='" & txtCA2.Text & "', FinalExam='" & txtFinalExam.Text & "' where ID='" & txtID.Text & "'", dbconn)

旁注 - 请注意您传递的文本 - 例如,如果 txtSName 在字符串中包含 ',它将引发错误。 检查This Stackoverflow ArticleMicrosoft SQL Parameters 他们都可以帮助你进行这种类型的编码。

【讨论】:

    【解决方案2】:

    据我所知,您在以下行中缺少您的结束 '

    adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1'" & txtCA1.Text & "', CA2'" & txtCA2.Text & "', FinalExam'" & txtFinalExam.Text & "'where ID=" & txtID.Text & "", dbconn)
    

    应该是

    adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1'" & txtCA1.Text & "', CA2'" & txtCA2.Text & "', FinalExam'" & txtFinalExam.Text & "'where ID=" & txtID.Text & "'", dbconn)
    

    【讨论】:

    • 不,不是这样。还有其他错误,但不应该有一个
    • 为什么不应该有一个结束 ' 那里?他们在定义 WHERE 时打开它?
    • 他们不是。他们在 WHERE 子句之前关闭了一个。 where 子句不能用引号引起来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多