Software Bpath Network


Visual C++ And ODBC


Language: C++
Compiler: MS Visual C++ 5.0 SP3
Environment: Windows 9x/NT

What's this?


This example shows how easy is the integration between Visual C++ projects and database handlind via ODBC.

How does it work?


Download source code (28 kb)

If you want to develop a program that handles a database via ODBC, first create and populate your own database. Using ODBC, is not important what database engine you are using: thanks to the ODBC architecture, all you have to do to use another db engine is to change the DSN. In this example we will use Access database (mdb file); with Access you can easily create a database, create a table into the database and insert data into that table. In this example one table is enough for our convenience.
I suggest to you to create a database called DATABASE.MDB, it has to contain a table named MYTABLE. Now structure the table MYTABLE to contain one COUNTER field, two NUMERIC fields (long and double), one STRING field and one BOOLEAN field. Name them as you prefer (but don't forget that names).
We have to create the right DSN (Data Source Name): DSN (as its name suggests) is an UNIQUE name that refers to a database via the right db engine (these informations will be specified during the DNS creation).
Now let's create the DSN following these steps:


Let's execute our preferred(?) IDE: Visual C++.
Now, using AppWizard, create a new project (MFC based) with the name you chose; then make following choices in AppWizard's steps:


AppWizar creates the entire workspace, adding all the classes necessary to do the job. Now edit the one and only form relative to the view, adding an editbox for each numeric/string field and a checkbox for the boolean field.
Now call ClassWizard and select 'Member Variables' tab. Double click (add a variable) for each control you add selecting it from the combo that appears in the 'Add Member Variable' dialog. As you can see, scrolling the combo's elements, you can read names that look like the fields of the table you created. The 'Variable type' in this dialog shows the type of the variable associated with that field. In some cases you can change the type: i.e. number can be int, UINT, float, double and so on. As you can see, the list of fields name appears in the combo depends on the type of the control: of course is a nonsense links a checkbox with a text field (maybe...).
Press OK. After you create all variables you are ready to see this demo running. Press F5 and, if everything is ok, your first ODBC program works.
Press the buttons in the toolbar and see what's append.
Notes: YOU DO ALL OF THIS WRITING NO CODE!!!
Micro$oft makes a great job!
Of course this is only an example. Next step your program should be able to add a new record, or modify an existing record. Records could be deleted too.
Reading the code you must focalize the interaction between CRecorview and CRecordset: m_pSet (the foreign class for the view) represents the recordset (the table MYTABLE in our case) and it's a member of the view. The recordset is opened when the view is created and via RFX (analogue to DDX) the field is linked with recordset members. Via DDX each m_pSet data member is linked with its relative control in the view.
Hope this short example is enough to start ODBC understanding... and isn't enough to discourage you!


Now you can send me a comment or suggestions for next atricles. Thanks.
Feel free to contact me if you found an error (any kind) in this article.


Back to Articles Page