• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Database access in C++

doug reich

Some guy
Joined
4/23/08
Messages
684
Points
28
I have to write a DLL that makes a call to an Access .MDB database. I think I have the DLL part down (I found http://msdn.microsoft.com/en-us/visualc/bb838687.aspx very instructive), but I can't find a good tutorial on using OLE DB (optimal, they tell me), ODBC (suboptimal, they tell me) or even DAO (which is supposed to be deprecated).

What I have found so far:
1. Two books from a few years ago. These are geared towards earlier versions of VS, and don't cut to the chase sufficiently for my simple task (http://p83-apps.appl.cuny.edu.remot...set_number=032987&set_entry=000001&format=999 and http://p83-apps.appl.cuny.edu.remot...set_number=032992&set_entry=000001&format=999)

2. Some MSDN pages that are also a little dense for me (http://msdn.microsoft.com/en-us/library/ms713643(VS.85).aspx and http://msdn.microsoft.com/en-us/library/h81kkytb(VS.80).aspx)

3. Some old online tutorials whose code doesn't work in VS 2008 (ODBC from C Tutorial Part 1 and others I can't find now...)

Does anyone have a good resource for learning Access MDB database access from scratch? I have no problem doing this in VBA (boy they make it easy), and no problem with SQL, but C++ has eluded me thus far.

Thanks!
 
I have some code/books somewhere at home with samples. Send me an email to remind me.
 
Hi Christian,

Although they call it C++ code, isn't that really C code (the headers & procedural nature)? I was also hoping for something with a little more explanation; i.e. what are the headers doing? The dll import line? This one in main:

C++:
::CoUninitialize();
(I don't understand that syntax at all).

I'll look around those ADO Examples to see what I can learn.

EDIT: I realize that CoUniniitialize line is for the DLL.
 
While ENROLL seems like a good example, it is so far form what I am used to (I haven't written a windowing application before; haven't used MFC or COM before), I don't even know where to begin looking. Do you know of anything that's perhaps, 5 steps below that? I'm not averse to learning these topics, but I don't think I have a chance being thrown a stack of files without a map.
 
Doug, although it might seem like a trivial task. It is not that straight forward. COM/ADO might be a pain but migth be the easiest way to get what you want. Don't try to get to deep in COM because you are going to get lost.

Do you need to do this in C++? C# might be easier if it is a windows App. Again, email me tonigth and I can get you something.

I remember writing something in the past (circa 1999) based on ODBC and plain C calls inside of C++.
 
Doug, although it might seem like a trivial task. It is not that straight forward. COM/ADO might be a pain but migth be the easiest way to get what you want.

I was lulled into a false sense of security by the ease with which VBA does it. I'll take another crack with that COM/ADO as you suggest.

Do you need to do this in C++? C# might be easier if it is a windows App.
The bindings for the API I am using are only in C, so I guess I'm stuck with that. I also have to export a C library, but I'm guessing you can do that in C# as well as in C++.
 
Plain ODBC API, just found it:

Dr. Dobb's | A Lightweight C++ Wrapper for Microsoft's ODBC API | January 1, 2002

C# is definitly the easiest way, but I don't know the requirements. As far as COM/ADo goes you just have to follow the pattern described in the samples, no need to learn COM

Great! This looks very promising. I was about to say something along the lines of "Why does VBA and PHP, etc. have a simple interface for DBs and C++ doesn't?" Well, someone has thought of it...
 

When I download the example and try to compile, I get some compiler errors, (almost) exclusively due to different string types being expected. I changed the types to what the compiler was looking for, and it compiles OK. However, the code throws an exception when I try to connect to the DB.

I'll post my code when I get a chance, probably this weekend.

As far as COM/ADo goes you just have to follow the pattern described in the samples, no need to learn COM

I know; I couldn't distinguish between what different parts of the code were doing in order to adapt it.
 
I am working through the example Andrey posted. It works for me, but I have to make some modifications, probably due to different Visual Studio versions. See the following for my adjustments; assume otherwise that I closely followed their instructions.

(I didn't find their code for download anywhere. If you copy their source code from the page, you end up with all the line numbers. To get rid of them, you can highlight them in Visual Studio while holding down alt -- this will select by columns instead of row.)

1. I had to create a Windows Form Application type project. I didn't have a managed C++ option. I also had to add the /clr:oldSyntax switch to the compile line. Right click on your project, go to properties, configuration Properties, C/C++, Command Line and add that line there. (The compiler still gave me a warning, but it compiled OK.)

2. I had to put in a few #using namespace declarations in the header file (I put them after the other using declarations)

C++:
using namespace System::Data;
using namespace System::Data::OleDb;
3. I had to declare the following member variables of the class that they didn't mention.

C++:
    DataTable* m_pAuthorTable;
    OleDbDataAdapter* m_pDA;
4. If on the "New record" part of the program you get an exception, read on to create an OleDbCommandBuilder data member.

5. I changed "_tmain" to be "main".

6. The code does not work if you try to do an ADD with an empty database. That's just a missing feature, I suppose.

I have attached my code here as well as the database I used. Everything works peachy! Many thanks!
 

Attachments

  • AuthorDB.zip
    37.8 KB · Views: 31
As soon as I wrote "it's attached", I went and broke something... putting it back as it was right now.

What's very weird is that I changed the code to use a different database... and now I can't insert new rows! However, all I did was change the column names and the file location. I'll play with it a little more.

EDIT: Can't figure out what was wrong; the working code is now attached.
 
Back
Top