通过 VB.net 更新访问数据库时出现 INSERT / UPDATE 错误
INSERT / UPDATE error updating access database through VB.net
我正在尝试通过 VB 在访问数据库文件中更新/保存新记录。
当我 运行 应用程序并按下保存或更新按钮时,我收到 2 个错误:
Additional information: Syntax error in INSERT INTO statement.
Additional information: Syntax error in UPDATE statement.
谁能看出我的语法有问题?
我会附上代码和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
这是我的图形用户界面
据我所知,您在下一行中遗漏了结束符 '
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)
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 Whosebug Article or Microsoft SQL Parameters 它们都可以帮助您进行此类编码。
我正在尝试通过 VB 在访问数据库文件中更新/保存新记录。
当我 运行 应用程序并按下保存或更新按钮时,我收到 2 个错误:
Additional information: Syntax error in INSERT INTO statement.
Additional information: Syntax error in UPDATE statement.
谁能看出我的语法有问题?
我会附上代码和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
这是我的图形用户界面
据我所知,您在下一行中遗漏了结束符 '
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)
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 Whosebug Article or Microsoft SQL Parameters 它们都可以帮助您进行此类编码。