Reputation: 31
Im currently trying to insert data that exists within a Excel spreadsheet directly into an Access table using C#. Dont want to use Interop in order to avoid hung sessions etc.
Managed to get quite far down the track using OleDb and the following SQL:-
INSERT INTO [MS Access;DATABASE=C:\Reports.mdb].tbl_Limit SELECT 1,Limit FROM [rngLimit]" where rngLimit is a Excel Range name.
Err Message:= No value given for one or more required parameters.
Very close but it still doesn't work. Any ideas?
Upvotes: 1
Views: 1806
Reputation: 31
Managed to solve the problem due to trail and error. Firstly make sure that the connectionstring is refering to the Access Database within OLEDB, then make your SQL follow this format:-
[INSERT INTO] + [MS Access;DATABASE=" + AccessDatabaseName + "]." + AccessDestinationTableName + " SELECT " + ExcelFieldNames + " FROM [" + ExcelRangeName + "]";
As you would with a normal SQL insert statement, you have to make sure that the field FROM and TO count are equal ie INSERT INTO tbl_Name(col1,col3) values(col1,col2)
Upvotes: 1
Reputation: 10679
Two thoughts:
Do the column titles in the Excel spreadsheet match up exactly to the column names in the Access database?
Is rngLimit
the name of a named range in Excel (strictly, a Name
object which is a member of the Names
collection of the appropriate workbook) or is it just the name of a variable which refers to a Range
object? If it is the latter then you need to give the full address of the range in this format: [Sheet1$A1:D52]
. You can investigate the Worksheet.Name
and the Address
properties of the Range
object for that
Upvotes: 0