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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15051842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:29:36  来源:igfitidea点击:

Syntax error in INSERT INTO Statement in vb.net

vb.netvb.net-2010

提问by CrashOverride

I am getting and syntax error in insert into statement. IFstatement is working just fine just getting this error when it's trying to save the information

我在 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

Can somebody help me with this....

有人可以帮我解决这个问题吗....

回答by Guffa

You have 13 fields to insert values in, but only 11 values.

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

回答by Sebastian

You are trying to insert into 13 columns but provide only 11 values!

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

回答by noeladna

Use brackets and it will work fine.

使用括号,它会正常工作。

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 & "');"

回答by CrashOverride

had to change the position field to something else don't know why but it worked

不得不将位置字段更改为其他内容不知道为什么但它有效

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 & "');"