I have used OledbConnection in two places in my solutions, one that reads Access and another that reads Excel. The one that reads Access is fine, but the one that reads excel is giving me error - The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
This is my method where I read Excel and return Datatable.
string CONSTRSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=<FILENAME>;Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
using (OleDbConnection objConnection = new OleDbConnection(CONSTRSTRING.Replace("<FILENAME>", fullFileName)))
{
DataSet dsImport = new DataSet();
try
{
objConnection.Open();
DataTable dtSchema = objConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if ((null == dtSchema) || (dtSchema.Rows.Count <= 0))
{
throw new Exception("Excel doesn't contain any readable table/tab");
}
new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", objConnection).Fill(dsImport);
}
catch (Exception ex)
{
throw ex;
}
finally
{
objConnection.Close();
}
return dsImport.Tables[0];
}
Other thing is that if I run the code from VS Debug, it works, but it doesn't when run from the IIS. This only happens for this code, other one for connection access works fine.
ps reading access:
try
{
string sAccessConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Persist Security Info=False;";
using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(sAccessConnection))
{
con.Open();
//doing stuffs
con.Close();
}
}
catch (Exception ex)
{
throw ex;
}
EDIT No, It doesn't have answer there. There it says how to register Microsoft.ACE.OLEDB.12.0. My solution has it working for Access(which means, it is registered) but doesn't work for Excel file. The question is why it is different for Excel and Acces?
And also, if I install x64 Access Driver I get following excetion
Retrieving the COM class factory for component with CLSID {73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).