INSERT INTO 语句中的语法错误。在 VB.net 2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15963641/
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 13:14:12  来源:igfitidea点击:

Syntax error in INSERT INTO statement. in VB.net 2010

databasevb.netdata-bindingbindingsyntax-error

提问by Reign De Leon

I have been this problem for a week and searching every existing forum for an answer maybe this time that i post my own problem.

我已经被这个问题困扰了一个星期,并在每个现有的论坛上搜索答案,也许这次我发布了我自己的问题。

My problem was in saving a data in a database. I have a datagrid that was bind to it but appear nothing. I'm using .mdb access database. the mdb table name was tblinformation.

我的问题是在数据库中保存数据。我有一个绑定到它的数据网格,但什么也没出现。我正在使用 .mdb 访问数据库。mdb 表名是 tblinformation。

It appears that my problem was in INSERT INTO statement, because there was a msgbox appears that everytime i try to save a data from textbox. and lastly, I'm new to vb.net >..<

看来我的问题出在 INSERT INTO 语句中,因为每次我尝试从文本框中保存数据时都会出现一个 msgbox。最后,我是 vb.net 的新手>..<

btw here's my code:

顺便说一句,这是我的代码:



Imports System.Data.OleDb

Public Class frmbookinfo
Dim cnn As New OleDb.OleDbConnection

Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
    Try
        cnn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\AmosBooks_System\AmosBooks_System\database.mdb")
        Dim command As String
        command = "INSERT INTO tblinformation(title, author, isbn, category, edition, pages, language, yearofpublication, bcode, price) VALUES (@title, @author, @isbn, @category, @edition, @pages, @language, @yearofpublication, @bcode, @price)"
        cnn.Open()
        Dim cmd As OleDbCommand
        cmd = New OleDbCommand(command, cnn)
        cmd.Parameters.AddWithValue("@title", txttitle.Text)
        cmd.Parameters.AddWithValue("@author", txtauthor.Text)
        cmd.Parameters.AddWithValue("@isbn", txtisbn.Text)
        cmd.Parameters.AddWithValue("@category", txtcategory.Text)
        cmd.Parameters.AddWithValue("@edition", txtedition.Text)
        cmd.Parameters.AddWithValue("@pages", txtpages.Text)
        cmd.Parameters.AddWithValue("@language", cmblanguage.Text)
        cmd.Parameters.AddWithValue("@yearofpublication", dtyearpub.Text)
        cmd.Parameters.AddWithValue("@bcode", txtbcode.Text)
        cmd.Parameters.AddWithValue("@price", txtprice.Text)


        cmd.ExecuteNonQuery()
    Catch exceptionObject As Exception
        MessageBox.Show(exceptionObject.Message)
    Finally
        cnn.Close()
    End Try
End Sub

回答by Steve

The syntax error is caused by the word LANGUAGE in the field names.
This word is reserved in JET-SQL and thus should be enclosed in square brackets

语法错误是由字段名称中的单词 LANGUAGE 引起的。
这个词在 JET-SQL 中是保留的,因此应该用方括号括起来

command = "INSERT INTO tblinformation(title, author, isbn, category, edition, pages, " + 
          "[Language], yearofpublication, bcode, price) VALUES " + 
          "(@title, @author, @isbn, @category, @edition, @pages, " + 
          "@language, @yearofpublication, @bcode, @price)"