在MS Access中插入INTO语法错误

时间:2015-10-22 02:59:14

标签: vb.net ms-access insert-into

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

也许我在insert语句中混合了DB字段名称。

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()

由于

2 个答案:

答案 0 :(得分:1)

你应该在[]中包含包含空格的字段名称。而不是

SELECT ID
      ,TYPE
      ,TIMESTAMP
      ,QTY
FROM   PrefixTable t1
WHERE  NOT EXISTS (
           SELECT 1
           FROM   PrefixTable t2
           WHERE  SUBSTRING(t2.id ,1 ,6) = SUBSTRING(t1.id ,1 ,6)
                  AND t2.TIMESTAMP = t1.TIMESTAMP
                  AND t2.id < t1.id
       )

你需要

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

并在你的添加参数块中:

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

答案 1 :(得分:0)

看起来您的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参数需要与插入参数名称匹配。