【问题标题】:Syntax error in InsertCommand from OleDbCommandBuilder来自 OleDbCommandBuilder 的 InsertCommand 中的语法错误
【发布时间】:2015-01-06 13:01:26
【问题描述】:

我正在尝试使用此代码将新订单添加到我的数据库中。我以前使用过相同的代码,但现在不起作用。你能帮忙吗?

我正在使用 Microsoft Visual Basic 2008 express edition 和 Microsoft Access。

我有四张桌子。

    CurrentRowNo = 0
    ' Purpose: Add new Account record and assign its default values
    ' Clear the Account table in the DataSet ready for a new record to be added
    SandRDataSet.Tables("Stock").Clear()
    'Add a new record to the studnet table
    Dim driveletter As String = Application.StartupPath.Substring(0, 1)
    Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & driveletter & ":\raheem\Computing\Database\SandR.accdb")
    cn.Open()
    Dim daStock As New OleDbDataAdapter("SELECT * FROM Stock;", cn)
    Dim sqlcmdbldStudent As New OleDb.OleDbCommandBuilder(daStock)
    'Fill the database
    daStock.Fill(SandRDataSet, "Stock")

    Dim test As DataRow = SandRDataSet.Tables("Stock").NewRow

    test("Product/Service number") = txtProductNo.Text
    test("Product/ Service name") = txtProductName.Text
    test("minimum level") = txtMin.Text
    test("maximum level") = txtMax.Text
    test("Quantity") = txtQuantity.Text
    test("Price") = txtPrice.Text


    SandRDataSet.Tables("Stock").Rows.Add(test)

    sqlcmdbldStudent.GetInsertCommand()

    daStock.InsertCommand = sqlcmdbldStudent.GetInsertCommand

    daStock.Update(SandRDataSet.Stock)
    DisplayAccount()

    cn.Dispose()
    cn.Close()

【问题讨论】:

  • 您能否提供有关错误的更多详细信息
  • 欢迎来到 Stack Overflow!请花点时间仔细地查看How to Ask

标签: vb.net ms-access


【解决方案1】:

如果您的表中确实有该列名(空格和正斜杠),那么我建议您将 QuotePrefix 和 QuoteSuffix 设置为您的 OleDbCommandBuilder

Dim sqlcmdbldStudent As New OleDb.OleDbCommandBuilder(daStock)
sqlcmdbldStudent.QuotePrefix = "["
sqlcmdbldStudent.QuoteSuffix = "]"

这将强制命令生成器创建的字段名称用方括号括起来并由数据库引擎正确解释

【讨论】:

  • 我是否必须删除此部分 Dim test As DataRow = SandRDataSet.Tables("Stock").NewRow test("Product/Service number") = txtProductNo.Text test("Product/Service name ") = txtProductName.Text test("最低级别") = txtMin.Text test("最高级别") = txtMax.Text test("数量") = txtQuantity.Text test("价格") = txtPrice.Text跨度>
  • 不,您只需将这两个属性添加到您的 OleDbCommandBuilder。因此,当您调用 GetInsertCommand 时,您会收到一个命令文本,其中所有字段名称都正确括在方括号中
猜你喜欢
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 2013-11-02
  • 2013-12-01
  • 2015-04-17
  • 2013-01-25
  • 2017-07-05
  • 1970-01-01
相关资源
最近更新 更多