Hi you great specialists everywhere ...

I try to insert data from an obscure source, that I have structured into a datatable,
into an existing database table using ODBC.

I was inspired by this :
http://www.vbforums.com/showthread.php?t=532088 (at the bottom : "Took me way too long, but i finally got it")

So I came up with this :
vb.net Code:
  1. Dim dt As DataTable = Data.Clone
  2.         Dim r2 As DataRow
  3.         For Each r As DataRow In Data.Rows  'to make sure every row is newly added
  4.             r2 = dt.NewRow
  5.             r2.ItemArray = r.ItemArray
  6.             dt.Rows.Add(r2)
  7.         Next
  8.  
  9.         Dim ODBCDataAdapter As New OdbcDataAdapter
  10.         ODBCDataAdapter.InsertCommand = New OdbcCommand("")
  11.         Dim val As String = ""
  12.         Dim prmODBC As OdbcParameter
  13.         Dim t As OdbcType
  14.         For Each c As DataColumn In dt.Columns
  15.             ODBCDataAdapter.InsertCommand.CommandText &= "," & c.ColumnName
  16.             val &= ",?" & c.ColumnName
  17.             Select Case c.DataType.FullName
  18.                 Case GetType(Short).FullName
  19.                     t = OdbcType.SmallInt
  20.                 Case GetType(Integer).FullName
  21.                     t = OdbcType.Int
  22.                 Case GetType(Long).FullName
  23.                     t = OdbcType.BigInt
  24.                 Case GetType(String).FullName
  25.                     t = OdbcType.VarChar
  26.                 Case GetType(Boolean).FullName
  27.                     t = OdbcType.Bit
  28.                 Case Else
  29.                     t = Nothing
  30.             End Select
  31.             If t = OdbcType.VarChar Then
  32.                 prmODBC = ODBCDataAdapter.InsertCommand.Parameters.Add("?" & c.ColumnName, t, 50)
  33.             Else
  34.                 prmODBC = ODBCDataAdapter.InsertCommand.Parameters.Add("?" & c.ColumnName, t)
  35.             End If
  36.             prmODBC.SourceVersion = DataRowVersion.Current
  37.             prmODBC.SourceColumn = c.ColumnName
  38.         Next
  39.         ODBCDataAdapter.InsertCommand.CommandText = "Insert into " & Data.TableName & "(" & ODBCDataAdapter.InsertCommand.CommandText.Substring(1) & ") VALUES (" & val.Substring(1) & ")"
  40.  
  41. ==> in the debugger I check CommandText :
  42. "Insert into Venice_APR(YR,SysNum,DocNum,SupNum,AccYear,Book,AutoPay) VALUES (?YR,?SysNum,?DocNum,?SupNum,?AccYear,?Book,?AutoPay)"
  43.  
  44. 'then I continue :
  45.  
  46.             ODBCDataAdapter.Update(dt)
..... and get the error :
You have an error in your SQL syntax; check the manual for the right syntax to use near 'Book,1AutoPay)' at line 1

This I do not understand ...
Something wrong with the Book column ? defined as varchar(3)

The "data" datatable with the data has all the right data in the correct form,
I am sure of dat, because up to now I have obtained the wanted functionality in a different way :
looping through the rows, I issue separate insert-statements.
But the performance is rather poor, so I am exploring better ways to reach my goal ...

Thanks for any help offered !

Regards,
Jan (Belgium)