附加信息:INSERT INTO语句中的语法错误

时间:2014-03-26 12:14:11

标签: database ms-access syntax

我尝试将我的数据库(ms-access)连接到Visual Basic。但它出现了以下错误:

    A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

    Additional information: Syntax error in INSERT INTO statement.

    If there is a handler for this exception, the program may be safely continued.

我使用了以下代码。请查看是否有任何错误..请帮助我... 守则是:

    Private Sub frmGive_Load(sender As Object, e As EventArgs) Handles Me.Load
    con = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\AntivirUS Vandry\Documents\Visual Studio 2013\Projects\Give And Get\dbaseMain.mdb")
    Dim sql As String = "Select * from tblGive"
    Dim dbcmd As OleDbCommand = New OleDbCommand(sql, con)
    con.Open()
    Dim dbadapter As OleDbDataAdapter = New OleDbDataAdapter(sql, con)
    Dim db As DataSet = New DataSet("TABLE")
    dbadapter.Fill(db, "TABLE")
    'create new instance of table so that row can be accessed
    Dim dt As New DataTable
    dt = db.Tables("TABLE")
    CmbGenre.Text = dt.Rows(0)(0)
    CmbLanguage.Text = dt.Rows(0)(1)
    txtNMovie.Text = dt.Rows(0)(2)
    txtFName.Text = dt.Rows(0)(3)
    txtLname.Text = dt.Rows(0)(4)
    CmbClass.Text = dt.Rows(0)(5)
    txtnull.Text = dt.Rows(0)(6) 
End Sub

它们之间有一些代码。包括文本框和组合框。

    Public Sub submit()
    con = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\AntivirUS Vandry\Documents\Visual Studio 2013\Projects\Give And Get\dbaseMain.mdb")
    con.Open()

    Dim sql As String
    sql = "Insert into tblGive (Genre,Language,NMovie,FName,LName,Class,SaveDate)" + "VALUES (" & CmbGenre.Text & "','" & CmbLanguage.Text & "','" & txtNMovie.Text & "','" & txtFName.Text & "','" & txtLname.Text & "','" & CmbClass.Text & "','" & txtnull.Text & "')"
    MsgBox(sql)
    Dim dbcmd As OleDbCommand
    dbcmd = New OleDbCommand(sql, con)
    dbcmd.ExecuteNonQuery()
    MsgBox("Saved")
End Sub

1 个答案:

答案 0 :(得分:0)

您在values关键字的开头缺少单引号。

换句话说,

VALUES (" & CmbGenre.Text & "','" & CmbLanguage.Text &

应该是

VALUES ('" & CmbGenre.Text & "','" & CmbLanguage.Text &