INSERT INTO语句中的语法错误

时间:2015-02-27 18:59:25

标签: asp.net vb.net ms-access

我想在Access表中插入新值我正在使用此代码

Public cmd As New OleDbCommand
Public CMDB As New OleDbCommandBuilder
Dim da As OleDb.OleDbDataAdapter
Dim con As New OleDb.OleDbConnection

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

    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|learn_english.mdb"
    con.Open()
    cmd = New OleDbCommand("insert into quiz (question, option1, option2, option3, option4, answer, level) values(@question, @op1, @op2, @op3, @op4, @answer ,@level)", con)
    cmd.Parameters.AddWithValue("@question", TextBox1.Text)
    cmd.Parameters.AddWithValue("@op1", TextBox2.Text)
    cmd.Parameters.AddWithValue("@op2", TextBox3.Text)
    cmd.Parameters.AddWithValue("@op3", TextBox4.Text)
    cmd.Parameters.AddWithValue("@op4", TextBox5.Text)
    cmd.Parameters.AddWithValue("@answer", TextBox6.Text)
    cmd.Parameters.AddWithValue("@level",DropDownList1.SelectedValue)

    cmd.ExecuteNonQuery()
    con.Close()
End Sub

但我在INSERT INTO语句中收到错误"语法错误"

1 个答案:

答案 0 :(得分:0)

OleDbCommand不支持命名参数。您必须将@PARAM替换为?。 (请参阅MSDN中的OleDbCommand.Parameters。)

  

OLE DB .NET提供程序不支持传递的命名参数   SQL语句或由a调用的存储过程的参数   CommandType设置为Text时的OleDbCommand。在这种情况下,   必须使用问号(?)占位符。

cmd = New OleDbCommand("insert into quiz (question, option1, option2, option3, option4, answer, [level]) values(?, ?, ?, ?, ?, ? ,?)", con)
cmd.Parameters.Add(TextBox1.Text)
cmd.Parameters.Add(TextBox2.Text)
cmd.Parameters.Add(TextBox3.Text)
cmd.Parameters.Add(TextBox4.Text)
cmd.Parameters.Add(TextBox5.Text)
cmd.Parameters.Add(TextBox6.Text)
cmd.Parameters.Add(DropDownList1.SelectedValue)

我认为这段代码可以解决问题!