vb.net中INSERT INTO语句中的语法错误

时间:2013-02-24 12:44:58

标签: vb.net vb.net-2010

我在insert into语句中遇到语法错误。 IF语句正常工作,只是在尝试保存信息时出现此错误

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txt12Per.Text >= TextBox12.Text And txtGPer.Text >= TextBox11.Text And TextBox1.Text >= TextBox10.Text Then
            Try
                'Dim da As OleDb.OleDbDataAdapter
                Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Taher\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\Database1.accdb;Persist Security Info=False;"
                Me.con = New OleDb.OleDbConnection()
                con.ConnectionString = dbprovider
                con.Open()

                Dim sqlquery As String = "INSERT INTO MCAscheduled (URno,SName,hsc,gper,pgper,pstatus,cname,hrname,position,hscinter,ginter,pginter,comments)" + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "');"
                Dim sqlcommand As New OleDb.OleDbCommand(sqlquery)

                With sqlcommand
                    .CommandText = sqlquery
                    .Connection = con
                    .ExecuteNonQuery()
                    con.Close()
                    txtUrn.Text = ""
                    txt12Per.Text = ""
                    txtGPer.Text = ""
                    txtName.Text = ""
                    cmbNameofGCourse.Text = ""
                End With
                MsgBox("Record Added")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        Else
            MsgBox("Student Not eligible for the requested company")
        End If
    End Sub

有人可以帮我这个....

4 个答案:

答案 0 :(得分:3)

您有13个字段可以插入值,但只有11个值。

答案 1 :(得分:3)

您正尝试插入13列,但只提供11个值!

答案 2 :(得分:1)

使用括号,它可以正常工作。

Dim sqlquery As String = "INSERT INTO MCAscheduled **([URno],[SName],[hsc],[gper],[pgper],[pstatus],[cname],[hrname],[position],[hscinter],[ginter],[pginter],[comments])"** + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "');"

答案 3 :(得分:0)

必须将位置字段更改为其他不知道为什么但是有效的

Dim sqlquery As String = "INSERT INTO MCAscheduled (URno,sname,hsc,gper,pgper,pstatus,cname,hrname,hscinter,ginter,pginter,comments,post,course,pcourse)" + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "','" & ComboBox1.Text & "','" & cmbNameofGCourse.Text & "','" & TextBox5.Text & "');"