MS Access 中的 INSERT INTO 语法错误

Posted

技术标签:

【中文标题】MS Access 中的 INSERT INTO 语法错误【英文标题】:INSERT INTO syntax error in MS Access 【发布时间】:2015-10-22 02:59:14 【问题描述】:

我有这段代码...当 aspx.vb 尝试插入时,我收到语法错误,我检查了数据库(访问文件),发现一切正常。

也许我在插入语句中混合了数据库字段名称。

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim myConn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim sqlString, FName, LName, NT, ACD, Account, Clocka, Supervisor, Shift, Shiftnew, Days, Clock, Emp, Commm, HR, doff As String
    Dim MailContent As String = String.Concat("      Requestor     ", Name.Text, "   Agent ID    ", Last.Text, "    AGENT NT      ", NTE.Text, "      Account       ", DropDownList1.SelectedValue, "    Supervisor      ", DropDownList2.SelectedValue, "    Current Shift     ", DropDownList6.SelectedValue, "      Current Shift Start Time      ", DropDownList7.SelectedValue, "   New Shift       ", DropDownList4.SelectedValue, "     New Shift Start Time      ", DropDownList3.SelectedValue, "      Addendum sent to HR?      ", RadioButtonList1.SelectedValue, "     Days off    ", TextBox2.Text, "    Comments        ", TextBox1.Text)

    FName = Name.Text
    LName = Last.Text
    NT = NTE.Text
    ACD = ACDE.Text
    Account = DropDownList1.SelectedValue
    Supervisor = DropDownList2.SelectedValue
    Shift = DropDownList4.SelectedValue
    Clocka = DropDownList7.SelectedValue
    Emp = DropDownList5.SelectedValue
    Shiftnew = DropDownList6.SelectedValue
    Clock = DropDownList3.SelectedValue
    doff = TextBox2.Text
    HR = RadioButtonList1.SelectedValue
    Days = Calendar1.SelectedDate
    Commm = TextBox1.Text

    Response.Write("<script language=""javascript"">alert('Information submitted successfully!!!');</script>")

    myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WFM\Database4.accdb")

    myConn.Open()
    ' sqlString = "INSERT INTO Absence (FName, LName, Account, Supervisor, Tipo, Justification, Clock, Cuando) VALUES ( " + FName + "," + LName + "," + Account + "," + Supervisor + "," + Type + "," + Justification + "," + Clock + "," + Days + ")"

    sqlString = "INSERT INTO Absence (Requestor, Employee ID, NT, ACD, Account, Supervisor, Current Shift, Current Shift Start Time, Employee type, New Shift, New Shift Start Time, Days Off, HR, Shift Start, Comments) VALUES (@FName, @LName, @NT, @ACD, @Account, @Supervisor, @Shift, @Clocka, @Emp, @Shiftnew, @Clock, @doff, @HR, @Days, @Commm)"
    'sqlString = "INSERT INTO Absence (FName, LName, NT, ACD, Account, Supervisor, Shift, Clocka, Emp, Shiftnew, Clock, doff, HR, Days, Commm) VALUES (@Requestor, @Employee ID, @NT, @ACD, @Account, @Supervisor, @Current Shift, @Current Shift Start Time, @Employee type, @New Shift, @New Shift Start Time, @Days Off, @HR, @Shift Start, @Comments)"

    cmd = New OleDbCommand(sqlString, myConn)
    cmd.Parameters.AddWithValue("@Requestor", FName)
    cmd.Parameters.AddWithValue("@Employee ID", LName)
    cmd.Parameters.AddWithValue("@NT", NT)
    cmd.Parameters.AddWithValue("@ACD", ACD)
    cmd.Parameters.AddWithValue("@Account", Account)
    cmd.Parameters.AddWithValue("@Supervisor", Supervisor)
    cmd.Parameters.AddWithValue("@Current Shift", Shift)
    cmd.Parameters.AddWithValue("@Current Shift Start Time", Clocka)
    cmd.Parameters.AddWithValue("@Employee type", Emp)
    cmd.Parameters.AddWithValue("@New Shift", Shiftnew)
    cmd.Parameters.AddWithValue("@New Shift Start Time", Clock)
    cmd.Parameters.AddWithValue("@Days Off", doff)
    cmd.Parameters.AddWithValue("@HR", HR)
    cmd.Parameters.AddWithValue("@Shift Start", Days)
    cmd.Parameters.AddWithValue("@Comments", Commm)

    cmd.ExecuteNonQuery()
    myConn.Close()

谢谢

【问题讨论】:

【参考方案1】:

并且您应该将包含空格的字段名称括在 [] 中。所以不是

insert into ....... Current Shift, Current Shift Start Time, ....

你需要

insert into ....... [Current Shift], [Current Shift Start Time] 

在您的添加参数块中:

...                           
cmd.Parameters.AddWithValue("@Shift", Shift)
cmd.Parameters.AddWithValue("@Clocka", Clocka)
'            Parameter Name: ^         ^ your variable name
...

【讨论】:

【参考方案2】:

看起来您的 INSERT 参数与参数列表不匹配,例如,您有 @FName,但是在您的参数列表中您没有定义 @FName 参数:

sqlString = "INSERT INTO Absence (Requestor, Employee ID, NT, ACD, Account, Supervisor, Current Shift, Current Shift Start Time, Employee type, New Shift, New Shift Start Time, Days Off, HR, Shift Start, Comments) VALUES (@FName, @LName, @NT, @ACD, @Account, @Supervisor, @Shift, @Clocka, @Emp, @Shiftnew, @Clock, @doff, @HR, @Days, @Commm)"

cmd参数需要和insert参数名称匹配。

【讨论】:

以上是关于MS Access 中的 INSERT INTO 语法错误的主要内容,如果未能解决你的问题,请参考以下文章

在 MS-Access 中生成 INSERT INTO 语句

MS Access 使用 SQL INSERT INTO - SELECT FROM 插入记录

MS Access 的 SQL INSERT INTO 语句

MS-Access:SQL JOIN 和 INSERT INTO 与 WHERE 慢

SQL INSERT INTO SUBFORM from QUERY in ms access

MS Access db.Execute " INSERT INTO