我在 vb.net 中的查询出现插入错误

I am having an Insert into error with my query in vb.net

我正在尝试使用插入查询将数据插入到 Access 数据库 table 中,我收到错误 INSERT INTO 语句中的语法错误。

我什至尝试过不带参数的 sql 字符串并得到相同的错误,但如果我将相同的查询直接粘贴到 Access 中它就可以工作。

Dim sqlinsert As String
'sqlinsert = "INSERT INTO JobApplicants(Title, Address, Postcode, Phone, Email, Position, Education) VALUES(@Title, @Address, @Postcode, @Phone, @Email, @Position, @Education)"
' sqlinsert = "INSERT INTO JobApplicants(Title, ApplicantName,Address, Postcode, Phone, Email, Position, Education) VALUES('" & comboTitle.Text.ToString & "','" & txtApplicantName.Text & "','" & txtAddress.Text & "','" & txtPostcode.Text & "','" & txtPhone.Text & "','" & txtEmail.Text & "','" & comboPosition.Text & "','" & comboEducation.Text & "')"
sqlinsert = "INSERT INTO JobApplicants(Title, ApplicantName,Address, Postcode, Phone, Email, Position, Education) VALUES('Mr','freed','12 high st','sa123er','01234567890','a@b.c','head','gcse')"
Dim cmd As New OleDbCommand(sqlinsert, con1)

cmd.Parameters.Add(New OleDbParameter("@Title", comboTitle.Text))
cmd.Parameters.Add(New OleDbParameter("@Address", txtAddress.Text))
cmd.Parameters.Add(New OleDbParameter("@Postcode", txtPostcode.Text))
cmd.Parameters.Add(New OleDbParameter("@Phone", txtPhone.Text))
cmd.Parameters.Add(New OleDbParameter("@Email", txtEmail.Text))
cmd.Parameters.Add(New OleDbParameter("@Position", comboPosition.Text))
cmd.Parameters.Add(New OleDbParameter("@Education", comboEducation.Text))

con1.Open()
cmd.ExecuteNonQuery()
con1.Close()

您将 7 个参数绑定到实际上没有任何占位符的准备好的语句。作为原始 SQL 的插入语句恰好是有效的,但我怀疑语句中不存在的绑定参数是错误的来源。因此,尝试在准备好的语句中使用占位符:

sqlinsert = "INSERT INTO JobApplicants (Title, ApplicantName,Address, Postcode, Phone, Email, [Position], Education) VALUES (@Title, @ApplicantName, @Address, @Postcode, @Phone, @Email, @Position, @Education)"
Dim cmd As New OleDbCommand(sqlinsert, con1)

cmd.Parameters.Add(New OleDbParameter("@Title", comboTitle.Text))
cmd.Parameters.Add(New OleDbParameter("@ApplicantName", txtApplicant.Text))
cmd.Parameters.Add(New OleDbParameter("@Address", txtAddress.Text))
cmd.Parameters.Add(New OleDbParameter("@Postcode", txtPostcode.Text))
cmd.Parameters.Add(New OleDbParameter("@Phone", txtPhone.Text))
cmd.Parameters.Add(New OleDbParameter("@Email", txtEmail.Text))
cmd.Parameters.Add(New OleDbParameter("@Position", comboPosition.Text))
cmd.Parameters.Add(New OleDbParameter("@Education", comboEducation.Text))

con1.Open()
cmd.ExecuteNonQuery()
con1.Close()

请注意,您实际上并未绑定申请人姓名的值。因此,我假设某处有一个名为 txtApplicant 的文本框,我们可以访问其文本 属性 以获取该值。

使用括号

Dim sqlquery As String = "INSERT INTO MCAscheduled **([URno],[SName],[hsc],[gper],[pgper],[pstatus],[cname],[hrname],[position],[hscinter],[ginter],[pginter],[comments])"** + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "');"

POSITION 是 Access 中的 reserved word

所以你必须在查询中使用方括号。

"INSERT INTO JobApplicants(Title, ApplicantName, Address, Postcode, Phone, Email, [Position], ..."