Sei sulla pagina 1di 6

An Introduction to ODBC.

Copyright © 2003, John Barnett

http://www.jrbarnett.freeserve.co.uk/
An Introduction to ODBC.

ODBC is one of a family of data access technologies provided by Microsoft. Visit


http://www.microsoft.com/data for more information.

ODBC stands for Open DataBase Connectivity. It is a client side cross platform data
communication standard developed by Microsoft and is used predominantly on the Windows
platform, although I am aware of implementations for some Unix platforms as well. I will
concentrate on the Windows systems here as this is by far the most common.

From the point of view of the system user/administrator, ODBC has three aspects:
• The ODBC driver
• The ODBC Administrator
• The ODBC Data Source (DSN)

I will go through each of these in turn.

The ODBC driver


An ODBC driver is a piece of software which knows how to communicate with a data provider.
This can be a relational database, a spreadsheet, a text file or any other potential source of data. It
provides a vendor independent way of communicating in a two way basis with the application by
any ODBC aware client application.
It is typically written by the software manufacturer and supplied with their host application.
However, third party drivers do exist. From the systems perspective, the driver is a set of DLL files
that call the ODBC DLLs.

Some common drivers such as text files are supplied as part of the operating system. If the driver is
required as part of a larger application, it will typically be installed as part of an application's client
together with related utilities.

The ODBC Administrator


The ODBC Administrator is the control centre for ODBC data sources and drivers and lets you add,
configure and delete data sources, view the installed drivers and ODBC key file information. Some
systems have an icon for this in the Control panel but others don't and I don't know why. If you
can't find an icon titled "ODBC Administrator" or "Data Sources", just choose Start -> Run, put the
filename in the box then hit OK. It should be somewhere in the system path. The filenames are
ODBCAD32.EXE for Win32 systems or ODBCADMN.EXE if you still have a Win3.x system or
16 bit applications.

The ODBC Data Source (DSN)


The data source is the focal point of ODBC. A data source is an instance of a driver with
connection details to a particular source of data and other preferences related to that source,
accessed via the "Configure" button on the ODBC Administrator. The only mandatory field
common across all sources is a name for the list, everything else is specific to the driver you use
and it may or may not be mandatory.

2
Typically for a client/server RDBMS the details would include the server or domain it is hosted on,
network protocol and the username and password to connect. If file based the details would include
a path to the file either as a drive mapping or a UNC name and username/password.

If you run the ODBC Administrator, you will find that there are three types of data source, each
with their own tab:

Entries in the User DSN tab are specific to your user profile. If you are on a network with roaming
profiles enabled they will follow you around as you login to various computers. However, the full
driver must be installed on each workstation for it to work.

Entries in the System DSN tab are available to anybody who logs into that computer (provided that
access rights permit).

Entries in the File DSN tab can be shared and easily copied between systems giving an easy way to
duplicate a connection, provided that the full drivers have been installed on each computer.

Entries of different types can have the same name (although you can't have two of the same type
with the same name). However, connection from an application to a data source it is done by name
rather than name and type. If there are multiple connections with the same name, the User data
source takes priority, then the System source, with the File coming last.

Connection from within a program either has details stored in a ConnectString with your
application code (such as MS Access), which will typically take the form of
"DSN=MyDSN;UID=Alf;PWD=passw0rd;" or you will be requested for a username, data source
and password when trying to connect.
The above example will connect to the data source called MyDSN as user Alf with password
passw0rd. Usernames and passwords can of course be omitted if the data source has them stored in
its preferences, or you wish to override the stored names.

Common ODBC client applications.


All of the Microsoft Office applications can use ODBC data sources to get data. The Microsoft
Query applet is also an ODBC client. Database reporting applications such as Crystal Reports use
ODBC to retrieve data. So do database application programming tools such as Powerbuilder and
4D, and database design tools such as ERWin and PowerDesigner.
Practically every single desktop database and spreadsheet application can use ODBC data sources
for input, and many other applications can do so as well. It is a very widespread standard.

Software Development using ODBC.


A software developer wanting to allow their application to use ODBC as a client need not write
code specific to their preferred data provider for what they want to, only write to the ODBC
Application Programming Interface to allow it to use any data source, and then let the user choose a
source.

If you are developing a server side application that could be used as a data provider, you would
need to write drivers that conform to the ODBC standard, available from the Microsoft Data web
site shown above.

3
Questions and Answers

Q: What is the Client/Server architecture?


A: It is the separation of the data storage in the engine hosted on the server from the client which
requests the data and transmits updates to the server.

Q: What do I mean by "Client side" and "Server side"?


A: This refers to the system whose CPU is used to retrieve the data. The client is the computer that
is using the data. The server is the machine hosting the database engine.
In some cases, they could be the same system (perhaps the PC runs a local database engine, or the
data source is a non RDBMS source such as a CSV file) but this is rare in commercial applications.
Remember though that a computer may be a server for one application, it could also be a client for
another application so the distinction isn't always as clear cut as that.

Q: Can an application be both a client and a server simultaneously?


A: Yes. It would need to provide data to other sources, and also allow itself to access data sources.

Q: Can ODBC server applications be hosted on non Microsoft operating systems?


A: Yes. There are many applications available that can connect to Windows PC’s running on a
variety of operating systems, including Unix, NetWare, VMS, mainframes and many others. They
can of course also be hosted on Windows servers as well. Remember that the ODBC is a client
side system, it doesn’t know or care what operating system the server is running.

Q: Are there any competitors to ODBC?


A: Borland has developed the BDE (Borland Database Engine) which works with its database
engines, but this isn't used nearly as much as ODBC.
Sun has a standard called JDBC (Java Database Connectivity). As you may guess, this is a cross
platform standard for use in software developed in Sun's Java programming language. It is a very
similar idea but works entirely via ConnectStrings mentioned earlier.

Q: How does ODBC work?


A: At a very superficial way, ODBC works in a similar manner to a telephone exchange, in that it
will attempt to connect a client and server together using known details. The subsystem in itself
will make no attempt to interpret, validate or correct the data going either way, it will instead relay
any error messages to the calling application.

Q: What are the differences between the 16 bit and 32 bit ODBC drivers?
A: Apart from the fact that 16 bit drivers were written for use under Windows 3.1, and 32 bit ones
for every version of Windows from 95 onwards, not a lot. Typically they will include the same
facilities and the 32 bit version is obviously preferable on newer machines, being a lot faster in
many cases.
However, 16 bit applications cannot use 32 bit drivers even if running on a 32 bit operating system,
and this is why I still included details of the 16 bit administration tool.

Q: Where are the data source details stored?


A: In systems using 32 bit ODBC, details of drivers are stored under
HKEY_LOCAL_MACHINE\Software\ODBC\ODBCINST.INI
System DSN details are stored under HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI
User DSN details are stored under HKEY_CURRENT_USER\Software\ODBC\ODBC.INI

4
File DSN's are stored (on my system running Windows 2000 Pro SP3) in C:\Program
Files\Common Files\ODBC\Data Sources. This can be changed through Tweak UI if you really
want to, but don't unless necessary.

For 16 bit ODBC drivers and data sources, details of drivers are stored in ODBCINST.INI and
details of DSN's are stored in ODBC.INI, both of which will be in your Windows directory.

Please always use the ODBC Administrator to edit and configure your data sources. Don't edit the
registry or INI files unless you are absolutely sure you know what you are doing and have a good
known working backup.

Q: How can I backup my data sources?


A: Assuming that the registry, INI files and the aforementioned directories are included as part of
your backup regime, there will be no problem.
You could copy the individual DSN files or export the sections of the registry for transfer to
another computer.

Q: What is an RDBMS?
A: Relational DataBase Management System. It encompasses anything that follows the late Dr.
Edgar (Ted) Codd's 12 Laws of database design, developed back in the 1970's. Many client/server
RDBMS systems are available, including Sybase, SQL Server/MSDE, Oracle, DB2, Ingres,
Informix, MySQL and PostgreSQL.
Desktop database engines (those that don't use a separate backend server) include Jet (used by MS
Access and Visual Basic), Paradox and dBase.

Q: What is this SQL thing you keep mentioning in various systems?


A: SQL is Structured Query Language. It is a cross platform (mostly) standard for database access
defined by the American National Standards Institute (ANSI), although each engine has its own
variations and very few engines follow the specification to the letter. As a result, SQL code may be
reasonably portable but it will often need a little tweaking when being moved between different
vendor’s engines.

Q: Where can I find more information about ODBC?


A: http://www.microsoft.com/data or search for ODBC with your favourite search engine.

Solutions to Common Problems

This section contains solutions to real life ODBC problems that the author has fixed during his
career.

Q: My data source shows up in the User DSN tab and is configured correctly. I can connect to it
through applications I run, but my ASP/PHP web page (or other server side application) can't
connect, giving errors. Help!
A: Remember what I wrote about User DSN's being part of your profile. Server side applications
such as IIS and Apache typically logon to the host server as a non interactive system user and as
such won't have access to your profile, which user DSN's are stored in. Recreate the DSN as a
System one and you will be fine (You will need Administrative level access to the server to create
or edit a System DSN).

5
Q: A colleague and I logon to the same PC at different times and get different results when we
view the contents of a data source. The data hasn’t changed between the times we logon and off.
Why?
A: You have either a system data source set which is being overridden by a User source, with
slightly different sources, or you each have a User source that have slightly different settings to
each other.

About the Author


I am an IT professional with extensive database design/development/administration experience, as
well as system, network and user support work.

Potrebbero piacerti anche