1

我已经这个问题一周了,并在每个现有的论坛中搜索答案,也许这次我发布了我自己的问题。

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

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

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


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
4

1 回答 1

3

语法错误是由字段名称中的单词 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)"
于 2013-04-12T07:06:58.373 回答