I am populating a DataSet with data from an excel sheet. I now need to insert this data into a table in an Access database that has identical structure.
Convention would dictate that I iterate over the rows of the DataTable and make an INSERT query for each, but I was wondering if there is a more efficient way to accomplish this? Perhaps something analogous to SqlBulkCopy for SQL Server?
Here is the code I have so far:
Dim connection As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath(path) & ";Extended Properties=Excel 12.0;")
Dim adapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$] WHERE EmpID IS NOT NULL", connection)
Dim results = New System.Data.DataSet
connection.Open()
adapter.Fill(results)
connection.Close()
connection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath("~/App_Data/Leaves.accdb"))
`Now, short of a loop, how can I insert results.Tables(0) into my database?
Thanks for your help.
You can use ADO, for example:
Where the connection is to the Access database. If you wish to use the whole sheet, refer to [Sheet1$] as your table, if you wish to use a named range, just refer to it by name.