为啥会出现“INSERT INTO 语句中的语法错误”?

Posted

技术标签:

【中文标题】为啥会出现“INSERT INTO 语句中的语法错误”?【英文标题】:Why do I get "Syntax error in INSERT INTO statement"?为什么会出现“INSERT INTO 语句中的语法错误”? 【发布时间】:2014-12-17 19:20:59 【问题描述】:
Private Sub Save_Record()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim sSQL As String = String.Empty

    Try
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandText = CommandType.Text

        If Me.txt_Forename.Tag = 0 Then
            sSQL = "INSERT INTO PlayerDatabase ( Age_Group, Surname, Forename, Rating, DOB, Address, Email, Position, Foot, Mins_Played, Goals, Assists, Yellow_Cards, Red_Cards)"
            sSQL = sSQL & "  VALUES(@Age_Group, @Surname, @Forename, @Rating, @DOB, @Address, @Email, @Position, @Foot, @Mins_Played, @Goals, @Assists, @Yellow_Cards, @Red_Cards)"

        Else
            sSQL = "UPDATE PlayerDatabase set Age_Group = @Age_Group, Surname = @Surname, Forename = @Forename, Rating = @Rating, DOB = @DOB, Address = @Address, Email = @Email, Position = @Position, Foot = @Foot, Mins_Played = @Mins_Played, Goals = @Goals, Assists = @Assists, Yellow_Cards = @Yellow_Cards, Red_Cards = @Red_Cards WHERE ID = @id"
            cmd.CommandText = sSQL
        End If


        cmd.Parameters.Add("@Surname", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txt_Surname.Text)) > 0, Me.txt_Surname.Text, DBNull.Value)
        cmd.Parameters.Add("@Forename", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txt_Forename.Text)) > 0, Me.txt_Forename.Text, DBNull.Value)

        cmd.Parameters.Add("@DOB", OleDbType.Date).Value = Me.dtp_DOB.Text
        cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = Me.txt_Address.Text
        cmd.Parameters.Add("@Age_Group", OleDbType.VarChar).Value = Me.cb_AgeGroup.Text
        cmd.Parameters.Add("@Rating", OleDbType.VarChar).Value = Me.cb_Rating.Text
        cmd.Parameters.Add("@Email", OleDbType.VarChar).Value = Me.txt_Email.Text
        cmd.Parameters.Add("@Position", OleDbType.VarChar).Value = Me.cb_Position.Text
        cmd.Parameters.Add("@Foot", OleDbType.VarChar).Value = Me.cb_Foot.Text
        cmd.Parameters.Add("@Mins_Played", OleDbType.VarChar).Value = Me.nup_MinsPlayed.Text
        cmd.Parameters.Add("@Goals", OleDbType.VarChar).Value = Me.nup_Goals.Text
        cmd.Parameters.Add("@Assists", OleDbType.VarChar).Value = Me.nup_Assists.Text
        cmd.Parameters.Add("@Yellow_Cards", OleDbType.VarChar).Value = Me.nup_YellowCards.Text
        cmd.Parameters.Add("@Red_Cards", OleDbType.VarChar).Value = Me.nup_RedCards.Text
        cmd.Parameters.Add("@ID", OleDbType.Numeric).Value = Me.txt_Forename.Tag
        cmd.ExecuteNonQuery()

        If Me.txt_Forename.Tag = 0 Then
            cmd.CommandText = "Select @@Identity"
            Me.txt_Forename.Tag = cmd.ExecuteScalar()
        End If
        MsgBox("Data has been saved.")

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

不知道我在这里做错了什么。这是从各种文本框、组合框等添加到 Access 数据库。此过程在按下具有输入的表单上的按钮时运行。我是 vb.net 和一般编程的初学者,所以如果很明显,我很抱歉。

谢谢

【问题讨论】:

【参考方案1】:

单词 POSITION 在 MS-Access Jet Sql 中是保留的。这就是语法错误的原因。 如果要将其用作列或表的名称,则需要将其放在方括号之间

sSQL = "INSERT INTO PlayerDatabase ( Age_Group, Surname, Forename, Rating, DOB, " & _ 
      "Address, Email, [Position], Foot, Mins_Played, Goals, Assists, Yellow_Cards, Red_Cards)"
....
sSQL = "UPDATE PlayerDatabase set Age_Group = @Age_Group, Surname = @Surname, " & _
       "Forename = @Forename, Rating = @Rating, DOB = @DOB, Address = @Address, " & _ 
       "Email = @Email, [Position] = @Position, Foot = @Foot, Mins_Played = @Mins_Played, " & _ 
       "Goals = @Goals, Assists = @Assists, Yellow_Cards = @Yellow_Cards, " & _ 
       "Red_Cards = @Red_Cards WHERE ID = @id"

其中一部分你有另一个问题。 OleDb 无法通过名称识别参数。通常您应该使用问号而不是名称,但 Access 允许这样做可能是为了向它的大表亲 Sql Server 提供某种可移植性。在任何情况下,您都应该按照命名占位符出现在查询中的相同顺序在 OleDbCommand 集合中添加参数。所以你需要这个订单:

cmd.Parameters.Add("@Age_Group", OleDbType.VarChar).Value = Me.cb_AgeGroup.Text
cmd.Parameters.Add("@Surname", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txt_Surname.Text)) > 0, Me.txt_Surname.Text, DBNull.Value)
cmd.Parameters.Add("@Forename", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txt_Forename.Text)) > 0, Me.txt_Forename.Text, DBNull.Value)
cmd.Parameters.Add("@Rating", OleDbType.VarChar).Value = Me.cb_Rating.Text
cmd.Parameters.Add("@DOB", OleDbType.Date).Value = Me.dtp_DOB.Text
cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = Me.txt_Address.Text
cmd.Parameters.Add("@Email", OleDbType.VarChar).Value = Me.txt_Email.Text
cmd.Parameters.Add("@Position", OleDbType.VarChar).Value = Me.cb_Position.Text
cmd.Parameters.Add("@Foot", OleDbType.VarChar).Value = Me.cb_Foot.Text
cmd.Parameters.Add("@Mins_Played", OleDbType.VarChar).Value = Me.nup_MinsPlayed.Text
cmd.Parameters.Add("@Goals", OleDbType.VarChar).Value = Me.nup_Goals.Text
cmd.Parameters.Add("@Assists", OleDbType.VarChar).Value = Me.nup_Assists.Text
cmd.Parameters.Add("@Yellow_Cards", OleDbType.VarChar).Value = Me.nup_YellowCards.Text
cmd.Parameters.Add("@Red_Cards", OleDbType.VarChar).Value = Me.nup_RedCards.Text

只有当你有 UPDATE 路径而不是 INSERT 时,才应该添加最后一个参数。 (假设 ID 列是 AutoIncrement 一)

If Me.txt_Forename.Tag <> 0 Then
   cmd.Parameters.Add("@ID", OleDbType.Numeric).Value = Me.txt_Forename.Tag
End If

当您尝试读取@@IDENTITY 值时,最后还有另一个问题。使用相同的命令也可以,但是需要清除参数集合

   If Me.txt_Forename.Tag = 0 Then
        cmd.Parameters.Clear()
        cmd.CommandText = "Select @@Identity"
        Me.txt_Forename.Tag = cmd.ExecuteScalar()
    End If

【讨论】:

以上是关于为啥会出现“INSERT INTO 语句中的语法错误”?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 GoogleSignInAccount 会出现此错误?

为啥会出现死锁?

为啥解密时会出现“BadPaddingException”?

为啥会出现“数字格式异常”?

为啥python中会出现这个AttributeError?

为啥删除 UITableView 行时会出现错误?