问题
Forgive me for asking this stupid question but... I really cannot figure out how to solve this syntax error
In my database(which is access 2013) I have this fields in my table
Fields - Data Types
NO - Number
RANK - Short Text
LAST NAME - Short Text
FIRST NAME - Short Text
MIDDLE NAME - Short Text
ADDRESS - Short Text
CONTACT NUMBER - Short Text
ACCNT CODE - Short Text
UNIT CODE - Number
ATM ACCNT NUMBER - Number
PENSION TYPE - Short Text
REMARKS - Short Text
Here is my code:
Imports System.Data.OleDb
Public Class Form2
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim con As New OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Retiree.accdb"
Dim cmd As New OleDb.OleDbCommand
If Not con.State = ConnectionState.Open Then
con.Open()
End If
cmd.Connection = con
cmd.CommandText = "INSERT INTO Retiree(NO, RANK, [LAST NAME], [FIRST NAME], [MIDDLE NAME], ADDRESS, [CONTACT NUMBER], [ACCNT CODE], [UNIT CODE], [ATM ACCNT NUMBER], [PENSION TYPE], REMARKS) VALUES(399, 'dfdGDFGF', 'GwejjweDFGF', 'GDfgfgFGF', 'GDFGfgfF', 'GDFfdftertGF', 'GfgdfggfgfgDFGF', 'GdfggfdgvbvDFGF', 1323, 2132, 'GDFgdsfbvsdGF', 'GDsgdfsjjjFGF')"
cmd.ExecuteNonQuery()
con.Close()
Me.Close()
End Sub
End Class
The error message I get is: Syntax error in INSERT INTO statement.
I hope someone can point out the syntax error in my code because I really cannot figure out where is it.(Maybe my eyesight is really bad now)
回答1:
Try to use this. All column names put In []
brackets. There can be issue that because No
is reserved word, It should be inside []
.
cmd.CommandText = "INSERT INTO Retiree([NO], [RANK], [LAST NAME], [FIRST NAME], [MIDDLE NAME], [ADDRESS], [CONTACT NUMBER], [ACCNT CODE], [UNIT CODE], [ATM ACCNT NUMBER], [PENSION TYPE], [REMARKS]) VALUES(399, 'dfdGDFGF', 'GwejjweDFGF', 'GDfgfgFGF', 'GDFGfgfF', 'GDFfdftertGF', 'GfgdfggfgfgDFGF', 'GdfggfdgvbvDFGF', 1323, 2132, 'GDFgdsfbvsdGF', 'GDsgdfsjjjFGF')"
来源:https://stackoverflow.com/questions/28895789/syntax-error-in-insert-into-statement-vb-and-access-2013