1

I've looked through an awful lot of questions/answers here, I'm sure this has to have been asked before but I can't find it as it applies to my exact situation.

I have a system.data.datatable from a generic source (could be anything), and I need to be able to export it to Access. The original source may in fact be sql, excel, etc., but it's been stored in a system.data.datable successfully (no problem).

In code, I create a new Access DB .accdb file, create the table by looping through the columns and translating the data types. This executes successfully.

Then we get to the code that is problematic:

  Try

        dt.TableName = tableName
        Using accConnection As New OleDb.OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;", dbPath))

            'first create table
            CreateTable(accConnection, dt, tableName)

            'set up dataadapter to copy data over
            Dim selectCommand As String = String.Format("SELECT * From {0}", tableName)
            Dim accDataAdapter As New OleDb.OleDbDataAdapter(selectCommand, accConnection)
            'accDataAdapter.TableMappings.Add(tableName, tableName)

            Dim accCommandBuilder As New OleDb.OleDbCommandBuilder(accDataAdapter)
            accDataAdapter.InsertCommand = accCommandBuilder.GetInsertCommand
            'accDataAdapter.UpdateCommand = accCommandBuilder.GetUpdateCommand
            CleanUpInsertCommand(accDataAdapter.InsertCommand)
            'CleanUpUpdateCommand(accDataAdapter.UpdateCommand)

            accDataAdapter.Update(dt.Copy)

        End Using

    Catch ex As Exception
        Utility.HandleExceptionWithDefaults(ex, String.Format("Copying {0} to {1}", tableName, dbPath))
        Return False
    End Try

This seems to work fine up until the final .Update(dt.copy) call, where I get an insert syntax error. The 'CleanUpInsertCommand' simply adds brackets to the column names (since they may include spaces, keywords, or whatever a user may have entered), and does not appear to be the cause. Here is the InsertCommand.CommandText in my current test after manipulation prior to the update call:

INSERT INTO Addendum_A_Preview ([variablename1] , [variablename2], [variablename3], [variablename4], [variablename5], [variablename6], [variablename7], [variablename8]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)

Which looks fine, as far as I know, since I assume the update fills the values '?' with the appropriate value during the Update call. If I'm wrong, please correct me (the update call is a bit of a blackbox to me, I'd rather not breakout reflector on it).

I can't see what's causing the issue though, and I'm getting this error: Exception: Syntax error in INSERT INTO statement. Type: System.Data.OleDb.OleDbException

Please help. I'll be beating my head against the keyboard until someone answers.

Calling Code:

    Dim dt As DataTable = DirectCast(gridData.DataSource, DataTable)
    Dim dbPath As String = String.Format("{0}\{1}.accdb", FileIO.SpecialDirectories.Desktop, _outputFileName) 'FileIO.SpecialDirectories.Desktop, _outputFileName)

    AccessHelper.CreateNewDatabase(dbPath)
    AccessHelper.AddTableToDatabase(dbPath, dt, _outputFileName.Replace(" "c, "_"c))

Last line calls the code in question.

EDIT: Solution ended up being my table name was not in brackets. Thanks all!

Mike
  • 43
  • 5
  • @Steve, Yes, as noted, the Cleanup simply adds the brackets since I need to be flexible in regards to what columns they might be including (internal users, but it's for a generic import process where a non-programmer user is previewing the data in Access). My accDataAdapter.InsertCommand.CommandText in my test data is like this after: `INSERT INTO Addendum_A_Preview ([variablename1] , [variablename2], [variablename3], [variablename4], [variablename5], [variablename6], [variablename7], [variablename8]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)` – Mike Apr 04 '17 at 15:48
  • Can you show where dt is instantiated? (Also, you're correct regarding the '?' values.) Are you 100% positive the data types align from the DataTable to the Database? (Dates in access are a nightmare sometimes.) – Kade M. Apr 04 '17 at 15:59
  • Try and insert a single row into the database after creating the table automatically. – Kade M. Apr 04 '17 at 16:04
  • Editing question now – Mike Apr 04 '17 at 16:05
  • In the test data, I'm only passing strings, and it's being translated to long texts (verified programmatic table creation is working). – Mike Apr 04 '17 at 16:08
  • I'd like to see the results of something like: Dim insert As New OleDb.OleDbCommand("INSERT INTO table (col1, etc) VALUES ([@var1], [@var2])", connection) insert.Parameters.Add("@var1", OleDb.OleDbType.VarChar, 20, "Variable 1") insert.Parameters.Add("@var2", OleDb.OleDbType.VarChar, 20, "Variable 2") insert.Parameters.Add("@var3", OleDb.OleDbType.VarChar, 20, "Variable 3") (I'm Still figuring out how to format comments... apologies.) – Kade M. Apr 04 '17 at 16:12
  • @KadeM. - how do you propose testing a single insert? I'm not amazing with Access/OledbDataAdapters, I'm normally in SQL usp land, so this is a bit out of my comfort zone. I'm messing around in intellisense here and nothing is jumping out at me as an easy 'executenonquery' substitute... EDIT: Posts crossed streams, thanks for the example, lemme try it out. – Mike Apr 04 '17 at 16:13
  • I added something above. (If anyone reading this can teach my how to format comments, you'll be my hero.) You'll run the above as insert.ExecuteNonQuery() – Kade M. Apr 04 '17 at 16:16
  • 1
    You shouldn't need to manually "clean up" the InsertCommand; you can use the `QuotePrefix` and `QuoteSuffix` properties of the CommandBuilder object to do that for you (ref: [here](http://stackoverflow.com/a/19562782/2144390)). – Gord Thompson Apr 04 '17 at 16:16
  • @GordThompson Thanks, that set me on the right path, the only difference between the quote suffix and prefix and my method was encapsulating the table name in brackets as well, which, wonder of wonderment, seems to have solved my error. How do I rep you? – Mike Apr 04 '17 at 16:22
  • @KadeM. thanks for your help too. I think the formatter you are looking for is the weirdly shaped single quote. `it does this` I copied it from the mini-markdown formatting blurb below... – Mike Apr 04 '17 at 16:24
  • That's useful... Yea, I was reading the blurb actually and it said that a double space at the end = new line, 4 spaces prior = code, etc. I couldn't get it working lol. Glad you got your code going though, happy I could help however I could. – Kade M. Apr 04 '17 at 17:32

0 Answers0