我正在尝试通过VB更新/保存访问数据库文件中的新记录。
当我运行应用程序并按下保存或更新按钮时,我收到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
这是我的GUI https://www.joelonsoftware.com/2006/06/16/my-first-billg-review/
答案 0 :(得分:0)
据我所知,你错过了你的关闭'在以下一行
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)
答案 1 :(得分:0)
詹姆斯几乎在那里,但我认为这应该为你做...
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 Article或Microsoft SQL Parameters他们都可以帮助您进行此类编码。