Error Using OleDbConnection

I am trying to connect with the below string but it gives a compile error. Weird portion is, it works fine on one machine but throws error on another machine!!! Below is my code and my error, can someone point out what I should change:

database = "\\\\Mithril\\Databases\\Master.mdb";
System.Collections.Hashtable lookup = new System.Collections.Hashtable();
//This is the error line
OLEDBConnection olecon = new OLEDBConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + database.ToString());

And here is the compile error I am getting:

Error 2 Cannot create an instance of the abstract class or 
interface 'Microsoft.Office.Interop.Excel.OLEDBConnection'  

EDIT --------------------

I have also tried:

using System.Data;
System.Data.OleDb.OLEDBConnection olecon = new System.Data.OleDb.OLEDBConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + database.ToString());

And this presents a different compile error:

Error 19 The type or namespace name 'OLEDBConnection' does not exist in the 
namespace 'System.Data.OleDb' (are you missing an assembly reference?

Upvotes: 0

Views: 2924

Answers (1)

Justin
Justin

Reputation: 86789

It looks like your code is referencing theMicrosoft.Office.Interop.Excel.OLEDBConnection interface, where you probably instead want the System.Data.OleDb.OleDbConnection interface.

The excel interop interface will only build correctly on machines where the required assembly is available (which is probably will be if Excel is installed), while the System.Data.OleDb interface is part of the .Net framework and so will work on all machine where the .Net framework is installed.

If you did intend to use the excel interop interface then it would be a good idea to add the required interop assemblies to some sort of "lib" directory alongside your source and reference it from there instead. See Compiling builds that have Microsoft Excel in TFS

Upvotes: 3

Related Questions