Converting tables from .mdbs to .accdbs by creating new .accdb with same tables and the tables have same column names with same column data types. Using OleDB and sql to ExecuteNonQuery. I am having problems when I try to have the same datatype from the old data table column for the new column. I bring fill dTable with
adp.Fill(dTable)
The following code was giving me issues with columns of different data types other than varchar.
Public Sub ConvertTable(dTable As DataTable, tableName As String)
Dim i As Int16 = 0
Dim j As Int16 = 0
strConnOutput = SharedRoutines.GetAccessACEConnectionString(aceDatabase)
Using con2 As New OleDbConnection(strConnOutput)
con2.Open()
Dim sqlText As String = ""
'Set up query to create a table with the same name as the'
'spreadsheet.'
sqlText = "CREATE TABLE [" & tableName & "] ("
'Create all the columns that exist in the spreadsheet.'
For Each col As DataColumn In dTable.Columns
sqlText &= "[" & col.ColumnName & "] varchar, "
Next
'Clean up the end of the CREATE query.'
sqlText = sqlText.Substring(0, sqlText.Length - 2) & ")"
Using cmValid As New OleDbCommand(sqlText, con2)
cmValid.ExecuteNonQuery()
End Using
'Set the INSERT SQL query for the data in the spreadsheet.'
sqlText = "INSERT INTO [" & tableName & "] ("
'Add all the columns to the INSERT query.'
For Each col As DataColumn In dTable.Columns
sqlText &= "[" & col.ColumnName & "], "
Next
'Clean up the end of the query string and prepare for the parameters.'
sqlText = sqlText.Substring(0, sqlText.Length - 2) & ") VALUES ("
'Add all the parameter placeholders to the INSERT query.'
For Each col As DataColumn In dTable.Columns
sqlText &= "?, "
Next
'Clean up the end of the INSERT query.'
sqlText = sqlText.Substring(0, sqlText.Length - 2) & ")"
Try
'Loop through all the rows of data in the spreadsheet.'
For Each rw As DataRow In dTable.Rows
Using cmValid As New OleDbCommand(sqlText, con2)
'Add a parameter for each column.'
For Each col As DataColumn In dTable.Columns
cmValid.Parameters.Add(New OleDbParameter(col.ColumnName, rw(col.ColumnName)))
Next
'Send data to the new table.'
cmValid.ExecuteNonQuery()
End Using
Next
Catch ex As Exception
j += 1
convertResponse &= tableName & " from " & aceDatabase & " was not completed."
MessageBox.Show(tableName & " from " & aceDatabase & " was not completed. Message:" & ex.ToString)
End Try
dTable.Dispose()
i += 1
convertResponse &= "Table " & tableName & " was added to " & aceDatabase & ControlChars.CrLf
End Using
End Sub
I changed the For Each loop with
For Each col As DataColumn In dTable.Columns
sqlText &= "[" & col.ColumnName & "] " & col.DataType.ToString & ", "
Next
but now the executeNonQuery() is throwing a syntax error. Is it not col.dataType.Tostring? SqlText with varchar CREATE TABLE [BASIC_DESCRIPTION] ([ColumnName] varchar, [Order] varchar) Compared with CREATE TABLE [BASIC_DESCRIPTION] ([ColumnName] System.String, [Order] System.Int32) My question is what do I need to put in the sqlText to make sure that the new columns being created are the same type as the old table? The closest answer I found is this. Link