I’m trying to import some data from excel into a database . i got the following code from http://www.davidhayden.com/blog/dave/archive/2006/05/31/2976.aspx . I have a x86 arhitecture so that’s not the problem . when i run the code the program says at connection.Open(); that The ‘Microsoft .Jet.OLEDB.4.0’ provider is not registered on the local machine. Any ideas ?
string excelConnectionString = @"Provider=Microsoft
.Jet.OLEDB.4.0;Data Source=C://suc.xls;Extended
Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook
using (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
("Select ID,Data FROM [Data$]", connection);
connection.Open();
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=.;Initial Catalog=Test;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelData";
bulkCopy.WriteToServer(dr);
}
}
}
If you are running the code on a 64 bit machine, and its a web project, then open IIS, right click on the Application pool and click on Advanced settings. Set the second property from the top: Enable 32 bit application to True, that should fix the problem.