I am attempting to write a function which deletes the contents of a MS Access table and replaces it with that of a datatable that contains the same columns.
So far I have the following, however upon running the function I receive a result of 0, indicating no updated rows.
Public Function UpdateMyTableContents(ByVal tblNew As DataTable) As Integer
Dim oleConn As OleDb.OleDbConnection '| Create & Configure
oleConn = New OleDb.OleDbConnection(strConnection) '| connection object
Dim oleCmd As New OleDb.OleDbCommand() '| Create & Configure
oleCmd.Connection = oleConn '| command object
oleCmd.CommandText = "Delete * from tbl_MyTable"
oleConn.Open() '| Delete the old
Dim intDeletedRows As Integer = oleCmd.ExecuteNonQuery() '| table's contents.
oleConn.Close() '|
'Setup Data Adapter to write new contents
Dim oleDA As New OleDb.OleDbDataAdapter()
oleDA.InsertCommand = New OleDb.OleDbCommand("INSERT INTO tbl_MyTable ([Company], [Brand], [Description]) VALUES (@Company, @Brand, @Desc)", oleConn)
oleDA.InsertCommand.Parameters.Add("Company", OleDb.OleDbType.VarChar, 20, "Company")
oleDA.InsertCommand.Parameters.Add("Brand", OleDb.OleDbType.VarChar, 100, "Brand")
oleDA.InsertCommand.Parameters.Add("Desc", OleDb.OleDbType.LongVarChar, 1000, "Product Description")
'Let's do it
oleConn.Open()
dim result as integer = oleDA.Update(tblNew)
oleConn.Close()
return result
End Function
As well as not being entirely sure of what the integer in the .InsertCommand.Parameters.Add command is supposed to represent (I assumed a maximum field length?), I don't understand why the table does not populate with the new data.
Does anyone have any idea where I've gone wrong?