我正在尝试使用 SQL 命令将新记录插入数据库,但每次运行程序并尝试添加新记录时,我都会收到一个错误消息,告诉我“INSERT INTO”语句存在语法错误。我插入的数据存储在数组 + 结构中:
Structure Question
Dim QuestionName As String
Dim Question As String
Dim Ans1 As String
Dim Ans2 As String
Dim Ans3 As String
Dim Ans4 As String
Dim Difficulty As Integer
Dim CorrectAns As String
End Structure
Dim arrQuestion as Question
这是用于将记录插入数据库的子 im:
Try
Dim InsertComm As New OleDb.OleDbCommand
Dim dbAdap As New OleDb.OleDbDataAdapter
ConnectToDB()
Dim sqlInsert As String = "INSERT INTO questionDatabase(QuestionName, Question,
Answer 1, Answer 2, Answer 3, Answer 4, Correct answer,
Difficulty ID) VALUES(" & Chr(39) & arrquestion.questionname
& Chr(39) & ", " & Chr(39) & arrquestion.question & Chr(39) &
", " & Chr(39) & arrquestion.ans1 & Chr(39) & ", " & Chr(39)
& arrquestion.ans2 & Chr(39) & ", " & Chr(39) &
arrquestion.ans3 & Chr(39) & ", " & Chr(39) &
arrquestion.ans4 & Chr(39) & ", " & Chr(39) &
arrquestion.correctans & Chr(39) & ", " & Chr(39) &
arrquestion.difficulty & Chr(39) & ");"
InsertComm = New OleDb.OleDbCommand(sqlInsert, dbConn)
InsertComm.ExecuteNonQuery()
dbConn.Close()
Catch ex As Exception
MsgBox(Err.Description)
Finally
dbConn.Close()
End Try
我已经写了又改了很多次,用谷歌搜索它给我的错误,并试图复制人们在那里发布的解决方案,但我就是无法理解他们是如何编写代码的。任何帮助将不胜感激。