2

I am new to C# and I am developing an application that uses a small Access database. I tried to understand what to use, ADODB or OLEDB, but it seems I was unable to solve the question.

So, is it possible use only one of these libraries? Or it is normal use both?

For example, the usual stuff with databases:

string MyQuery = "SELECT * FROM MyTable";
Recordset rs = new Recordset();
rs.Open(MyQuery, MyConnection, CursorTypeEnum.adOpenKeyset, LockTypeEnum.adLockOptimistic);

uses all ADODB objects, until:

OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dt = new DataTable("MyTable");
adapter.Fill(dt, rs);

so I feed a DataGridView with the Datatable. The adapter is OLEDB, not ADODB. Is there a way to avoid OLEDB? So, I thought I got rid of ADODB and do everything in OLEDB, all went well until I wanted add tables at runtime:

ADOX.Catalog MyCat = new ADOX.Catalog();
MyCat.ActiveConnection = MyConnection;
ADOX.Table table = new ADOX.Table();
table.Name = "MyTable";
table.Columns.Append("ID", ADOX.DataTypeEnum.adInteger);
table.Columns["ID"].ParentCatalog = MyCat;
table.Columns["ID"].Properties["AutoIncrement"].Value = true;
table.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "ID");
table.Columns.Append("DateAdded", ADOX.DataTypeEnum.adDate);
table.Columns.Append("Name");
table.Columns.Append("Surname");
cat.Tables.Append(table);

where the catalog absolutely refuses an OLEDB connection and demands an ADODB one so I needed to rescue ADODB.

So, in the end, do I need to resign to use both or I am doing something wrong in the code samples I posted? Alternatives are welcomed, thanks in advance.

It would very nice for a newbie know which system to use and stick with it, knowing it can do everything it needs to be done.

1 Answers1

4

Taken from this answer by an MCC, MVP:

OLEDB is a Microsoft standard that defines a set of API (Application Interface) functions for accessing a database. It is a COM (Component Object Model) API that was a follow-up to the ODBC API. Typically OLEDB is used to create a database specific driver, known as a provider, that can be implemented by a higher level data access library such as ADO or ADO.NET.

ADO (ADODB) is a generic (COM) database library, that can be used by programming languages such as Visual Basic and C++ to access any type of database for which an OLEDB Provider has been developed. In this context, ADO is an OLEDB Consumer. It communicates with the OLEDB Provider, which in turn communicates with the database directly or a database server.

For example, to open an Access database, the Connection object of ADODB would specify the Jet OLEDB Provider in its connection string to open and subsequently communicate with the database:

ADODB.Connection cnn;

cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                            "Data Source=E:\My Documents\db1.mdb;" +
                            "Jet OLEDB:Engine Type=5";
cnn.Open();

To me this implies that unless you have a specific ADO provider you want to use it would be better / quicker to use OLEDB directly.

However OLE may require more development time, although ADO will be quicker to develop in but slower in performance than OLE.

Some leading questions: What is your connection string? (Your problem implies that you are using an ADODB provider in your connection string). What is ADOX ?

P.S. Some reference material for you:

Microsoft Access Performance Tips to Speed up Your Access Databases

Access connection strings

EDIT: Taken from here: "With version 2007 onwards, Access includes an Office-specific version of Jet, initially called the Office Access Connectivity Engine (ACE), but which is now called the Access Database Engine. This engine is fully backward-compatible with previous versions of the Jet engine, so it reads and writes (.mdb) files from earlier Access versions. It introduces a new default file format, (.accdb), that brings several improvements to Access, including complex data types such as multivalue fields, the attachment data type and history tracking in memo fields. It also brings security and encryption improvements and enables integration with Microsoft Windows SharePoint Services 3.0 and Microsoft Office Outlook 2007"

Also you may run into 64 bit issues with Jet...

"The driver is not part of the Windows operating system, but is available as a redistributable.[11] Previously the Jet Database Engine was only 32-bit and did not run natively under 64-bit versions of Windows."

For how to work with MS Access via C# have a read of this good tutorial If you scroll down to the section with the title "Access (accdb)" you will see the code uses ADOX.catalog.

EDIT2: For info on ADOX.Catalog read this "Open the catalog by setting the ActiveConnection property to an ADO Connection object or a valid connection string."

So try something like this... Taken from the post underneath the accepted answer by Gord Thompson

ADOX.Catalog cat;
ADOX.Table tbl;

cat.ActiveConnection = _
    "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=C:\Users\Public\Database1.accdb;"
Community
  • 1
  • 1
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • Thanks for the quick answer. Your questions: my connection string is "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydatabase.mdb"; while ADOX is Interop.ADOX = Microsoft ADO Ext. 2.8 for DDL and Security. So, are you saying I should stick with OLEDB? In that case I do not know how use a catalog to connect to an OLEDB connection, as I said in the 2nd part of code I posted. –  Arne Saknussemm Sep 23 '15 at 11:49
  • ADOX is for creating databases from scratch by code or for modifying the schema/design by code. Can be used to interrogate the details of a database schema also. – eric1825 Sep 23 '15 at 13:40
  • @ArneSaknussemm For a start use ACE (it superseded Jet)... the connection string info can be found at the top of https://www.connectionstrings.com/access/ SEE MY EDIT – Paul Zahra Sep 23 '15 at 13:43
  • @ArneSaknussemm The connection string I pointed you to is for version 12... there are other versions available e.g. 14 and 15... I believe there is a bit of a naming screwup so you would have to check for the latest version of the drivers, e.g. version 12 maybe later than version 14 ?!?!?!? bloody MS! – Paul Zahra Sep 23 '15 at 14:35
  • Even after installing the driver for ACE OLEDB 12 and changing the connection string provider to Provider=Microsoft.ACE.OLEDB.12.0, the ADOX catalog still refuses it if declared as OLEDB connection object. But well, my program works, my philosophical problem is I am forced to use ADODB somewhere and OLEDB somewhere else, and it seems I can not avoid it. –  Arne Saknussemm Sep 23 '15 at 19:53
  • @Paul Zahra - just not to leave any proposition unanswered, I read your link. But it does not uses catalog objects to create tables. It uses a direct SQL query ("CREATE TABLE ... etc"). –  Arne Saknussemm Sep 23 '15 at 21:00
  • @ArneSaknussemm What exactly is the error message you get? – Paul Zahra Sep 24 '15 at 07:57
  • @ArneSaknussemm It must be something else wrong, because you should be able to use Jet 12 and adox to create tables... see http://stackoverflow.com/questions/17039905/how-to-use-adox-to-connect-to-existing-access-database – Paul Zahra Sep 24 '15 at 08:00
  • @Paul Zahra Answering your previous 2 comments, the error I get when I try to assign an OLEDB connection to an ADOX catalog is a COM exception "Invalid cast". What I pointed in my previous comment is that I get this same error using JET 4 or ACE 12 in the connection string. --- I had already found and studied the link you provided in your last comment, and as you can see the author uses an ADODB object with the ADOX catalog (so, not an OLEDB object). –  Arne Saknussemm Sep 24 '15 at 09:27
  • @Paul Zahra By your comments, I think I need to summarize my problem. As I said, my program works, provided I use BOTH, ADODB and OLEDB. I would like use ONLY one of them, but I do not know if it is possible (that is what I am asking here). -- Example of this problem, my 1st piece of code showed a DataTable filled by an OLEDB adapter. Question 1: is it possible do it with ADODB? My 2nd piece of code showed I need an ADODB connection for the ADOX catalog. Question 2: is it possible use OLEDB? If the answer to these 2 question is NO, this means we are forced to use BOTH (ADODB and OLEDB). –  Arne Saknussemm Sep 24 '15 at 09:38
  • @ArneSaknussemm See EDIT2. You don't need to use an ADO connection, you can just specify the connection string for the catalog.ActiveConnection. – Paul Zahra Sep 24 '15 at 09:43
  • @Paul Zahra ...as stated in the comments in that page, it does not work (COM exception "Arguments of the wrong type") –  Arne Saknussemm Sep 24 '15 at 09:55
  • @ArneSaknussemm My bad for being scatty... busy trying to get a windows service that uses MEF, Silverlight and lots of other rubbish working grrr... It seems that if you want to use ADOX.catalog to create a table that wou must specify an ADO conn (high level of abstraction - all examples I can find use ADO conn) vs not using ADOX.catalog and specifying the sql to create table yourself using an OLE connection (lower level of abstraction), personally I would prefer OLE because of the control it provides, however as you are relatively new to C# perhaps ADO is the way for you to go. – Paul Zahra Sep 24 '15 at 10:58
  • @ArneSaknussemm (On the basis of going one route of ADO or OLE) Basically if you insist on ADOX.catalog you will have to go the ADO route, you can do that without using any OLE other than the provider. If you want to go the OLE route you cannot use ADOX.catalog and so will have to specify the SQL to create the table. – Paul Zahra Sep 24 '15 at 11:04