简体   繁体   中英

Inserting data from an Excel Spreadsheet into Microsoft Access using C#

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?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM