ODBC Basics

This is the first tutorial in the series that deals with database programming in win32asm. Database programming is becoming more important in the world of IT. We cannot ignore it anymore. However, there are so many types of databases in use today. If we have to learn the database file formats in order to implement win32asm database programming, it would take like eternity.

Fortunately, Microsoft has a technology that helps us tremendously in this regard. It's called ODBC which stands for Open Database Connectivity. In essence, it's a set of APIs, the same as Windows APIs, that deal specifically with database programming. That is, with ODBC APIs, you have a relatively uniform way of accessing a wide variety of databases.

How does ODBC work? What's its structure? You should get a clear picture of ODBC architecture before using it. There are four components in ODBC:

The center of all four components is the ODBC manager. You can think of it as your foreman. You tell him what work you want done and he conveys your desire to his workers (ODBC drivers) and gets the job done. If the workers have some messages for you, they tell the foreman (ODBC manager) and the foreman relays the messages to you. The workers know their job well thus they can get it done for you.

In this model, you don't talk directly with database drivers. You tell ODBC manager what you want. It's the job of the ODBC manger to translate your wish into reality by using the appropriate ODBC drivers. Each ODBC driver knows everything there is to know about the database it's designed for. Thus each component does what it does best, simplifying the jobs enormously.

Your program <----> ODBC manager<----> ODBC Drivers <----> Databases

ODBC manager is supplied by Microsoft. Check your Control Panel. If your machine has ODBC installed correctly, you'll find ODBC Data Sources (32-bit) applet there. As to ODBC drivers, Microsoft supplies several with their products. And you can always obtain new ODBC drivers from the database providers. Just by installing new ODBC drivers, your machine can utilize new databases it hasn't known about before.

ODBC APIs are simple to use, however, you need to know something about SQL and database such as the meaning of field, primary key, record, column, row etc. I have to assume you have some basics in database theory so I can get on with the mechanics of ODBC programming in win32asm. As you can see, ODBC manager tries to hide the implementation details from your program. That means it has to specify some standard interfaces for talking to your program and the ODBC drivers. ODBC drivers differ in their capabilities so there must be a way for our applications to find out whether an ODBC driver supports a particular feature. ODBC defines three levels of services called Interface Conformance Levels. They are Core, Level 1 and Level 2. Every ODBC driver must implement all features specified in the core level list where as they can choose to implement features in level 1 or level 2. From our applications' point of view, ODBC APIs are divided between those three levels. If a specific function is labelled as core, it means you can use it without checking whether it's supported by a particular ODBC driver you're using. If it's a level 1 or 2 function, you need to make sure that the ODBC driver supports it before using it. You can obtain the details of ODBC APIs from MSDN.

You need to know some ODBC terms before plunging into coding.

Below are the steps you usually perform when coding with ODBC:

  1. Connect to the data source
  2. Build and execute one or more SQL statements
  3. Examine the resulting records (if any)
  4. Disconnect from the data source

We will learn how to perform each step in the next tutorials.


[Iczelion's Win32 Assembly Homepage]