简体   繁体   中英

Extracting Excel data using Sql connection string

My intention here is to render the excel data in a table, using a datatable, so opened an odbc connection and loaded the datatable like this:

System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xls)};Driverid=790;Dbq=C:\Users\*******\Documents\Book1.xlsx;DefaultDir=C:\Users\******\Documents;HDR=YES);
conn.Open();
DataTable dataTable = new DataTable();
string con = "select * from [sheet1$]";
System.Data.Odbc.OdbcCommand cmd = new System.Data.Odbc.OdbcCommand(con, conn);
System.Data.Odbc.OdbcDataReader dr = cmd.ExecuteReader();
dataTable.Load(dr);

But insted getting an error like this

ERROR [IM014] [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

So actually whats' the root cause ?

This suggests

"changing the Platform target project setting from Any CPU to x86."

will help. The root cause is, as the error says, a mismatch between the database driver and your application.

These docs say

If you use the 64-bit odbcad32.exe to configure or remove a DSN that connects to a 32-bit driver, for example, Driver do Microsoft Access (*.mdb), you will receive the following error message: The specified DSN contains an architecture mismatch between the Driver and Application To resolve this error, use the 32-bit odbcad32.exe to configure or remove the DSN.

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