質問

I'm trying to delete all data from a table in an Access (*.mdb) database in VB.NET, then reinsert new data in it however I get the error "Syntax error in INSERT INTO statement" when executing the following code:

Imports System
Imports System.Data
Imports System.Data.SqlClient

...

    'test.mdb file
    'with a table "MyTable"
    'with two fields:
    '"name" (Memo), "value" (Memo)

    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = C:\Users\Max\Desktop\test.mdb"
    con.ConnectionString = dbProvider & dbSource
    con.Open()
    sql = "SELECT * FROM MyTable"

    'select data from database, fill and clear dataset to get the table schema
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "MyTable")
    ds.Clear()

    'add a new row in the dataset
    Dim dsNewRow As DataRow = ds.Tables("MyTable").NewRow()
    dsNewRow.Item("name") = "TESTNAME"
    dsNewRow.Item("value") = "TESTVALUE"
    ds.Tables("MyTable").Rows.Add(dsNewRow)

    'update the database
    Dim objCommandBuilder As New OleDb.OleDbCommandBuilder(da)
    da.Update(ds, "MyTable")
    ds.Dispose()
    da.Dispose()
    con.Close()
    con.Dispose()
役に立ちましたか?

解決

  1. Make a backup copy of your Access db file.
  2. Rename those two fields in MyTable: name to fld1; and value to fld2
  3. Adapt your VB.Net code to use those names.

You indicated those steps eliminated the error.

The problem was that both name and value are reserved words. In some situations, the Access db engine can accept reserved words as field names without complaining. However, it seems less forgiving when OleDb is involved.

Ideally, avoid reserved words for Access db object names. That linked page includes a link to a free utility (DbIssueChecker.zip) you can use to examine your Access db file for problem names and other issues.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top