为什么我在INSERT INTO语句中收到“语法错误”?

时间:2014-12-17 19:20:59

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

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 个答案:

答案 0 :(得分:0)

在MS-Access Jet Sql中保留了POSITION一词。这就是SYNTAX ERROR的原因 如果要将其用作列或表的名称,则需要将其放在方括号

之间
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

并且只有在没有INSERT的UPDATE路径时才应添加最后一个参数。 (假设ID列为AutoIncrement 1)

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