MS Access 中的 INSERT INTO 语法错误

INSERT INTO syntax error in MS Access

我有这段代码...当 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()

谢谢

您的 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 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
...