1. DAO(Data Access Object)
DAO is familiar to database programmers using Microsoft Access Basic or
Microsoft Visual Basic. DAO uses the Microsoft Jet database engine (A database
engine is the software in a DBMS that parses and executes SQL statements and
accesses the physical data) to provide a set of data access objects: database
objects, tabledef and querydef objects, recordset objects, and others. DAO works
best with .mdb files like those created by Microsoft Access, but you can also
access ODBC data sources through DAO and the Microsoft Jet database engine.
Advantages of DAO
Easy to use.
Workspace level support. (A workspace is DAO's notion of a connection between
a user and a database. The workspace manages a list of databases that are active,
and of individual users or groups of users who are known to use the workspace. )
Both MFC and API provide DAO support.
Speed is relatively faster than ODBC for jet database engine databases.
Compatibility with the ODBC classes and with Microsoft Access Basic and
Microsoft Visual Basic.
Ability to specify relations between tables.
Databases You Can Access with DAO
Databases using the Microsoft Jet database engine, created with Microsoft
Access or Microsoft Visual Basic, versions 1.x, 2.x, and 3.0 of the database
engine.
Open Database Connectivity (ODBC) databases, including but not limited to
Microsoft SQL Server, SYBASE SQL Server, and ORACLE Server. To access an
ODBC database, you must have an appropriate ODBC driver for the
database you want to access.
Microsoft Excel, versions 3.0, 4.0, 5.0, and 7.0 worksheets.
Text files.
1
2. ODBC
The ODBC interface is an industry standard and a component of Microsoft
Windows Open Services Architecture (WOSA). The ODBC interface makes it
possible for applications to access data from a variety of database
management systems (DBMSs). ODBC permits maximum interoperability
(The ability of one application to use the same code when accessing data
in different DBMSs) -an application can access data in diverse DBMSs
through a single interface. Furthermore, that application will be
independent of any DBMS from which it accesses data. Users of the
application can add software components called drivers, which
create an interface between an application and a specific DBMS.
ODBC defines a standard set of functions for data access and carries a
specification for which vendors can write drivers (A driver is a routine
library that exposes the functions in the ODBC API. Drivers are specific to
a single DBMS) that grant your application access to almost any of the
databases currently available.
Data Source
The data source name represents a specific database or file that your ODBC application will
access. The database may be comprised of many different tables, and once you're connected to
the data source you can access any object within it.
• A data source is the data that the user wants to access and its associated
operating system, DBMS, and network platform (if any).
A data source name really is just that: a name. When you ask the driver manager to connect
you to a data source, it looks up the rest of the information it needs to get from your program
on your machine to the data source, wherever and on whatever machine that information lives.
ODBC specifies that each driver must implement a standardized set of SQL queries.
ODBC also standardizes the API used to access the database, which means that the SQL
commands and the calls you use to express those commands are interpreted in a common style
by the driver.
2
3. Examples of data sources include a remote database running on Microsoft SQL Server across a
network or a Microsoft Access file in a local directory.
ODBC Layered Architecture
ODBC has a layered architecture. At the top, it starts with your application, which is written to
call the ODBC APIs. Your application requests that a data source be opened by specifying a data
source name configured by the user.
The data source name provides the next layer, the driver manager, with enough information to
do its work. The manager loads the appropriate ODBC driver and initializes it for a connection
to the data source you've named. The driver manager passes on any subsequent calls to the
ODBC API, from your application to the driver. The driver actually implements the call, doing
the work required to retrieve or accept data, or performing housekeeping chores.
Databases You Can Access with ODBC
3
4. Using ODBC and the MFC ODBC classes, you can access any data source, local or remote, for
which the user of your application has an ODBC driver. 16-bit, 32-bit, and 64-bit ODBC drivers
are available for a wide range of data sources. If you are working with a Microsoft Jet (.mdb)
database, it is more efficient to use the DAO classes than the Microsoft Access ODBC driver.
Advantages of ODBC
Easy to use specially when developer are not familiar with COM environment.
SQL support.
Both API and MFC support.
Good for relational databases only.
DAO (Data Access Object)
Data access objects are exactly what their name implies: a set of Automation objects that allow
your application to gain entry to different databases.
Using DAO to work with native Microsoft Access databases provides the best performance .
Creating Data Sources
When you start working with ODBC, you'll always get started by using a data source.
When you create a data source in your application, you're really only creating a link between
the actual data source and ODBC.
Steps for setting up a default data source using ODBC
1. First step in creating an ODBC data source, as viewed through Control Panel, is to
double-click on the ODBC icon. This will bring up the ODBC Data Source Administrator
dialog box, as shown below:
4
5. 2. To create a new data source, just click the Add... Button ,Add Data Source Dialog Box will be
displayed. Choose Microsoft Access driver from the list of drivers displayed & click on OK.
ODBC Microsoft Set up Dialog box will appear which is shown below:
5
6. 3. Enter the data source name (that u’ve used in codes) and description and click on
Select.
4. In the Select database dialog box , from the list of databases select database you have
created.
5. Click on OK .you will return to previous dialog box of ODBC Microsoft Set up Dialog box
.
6. The data source has been set .Click Ok.
Now ,you will see again the startup dialog box with your DSN . Click close to complete the
process.
Steps to create building a Database Application using ODBC using AppWizard
1. Select File | New. This opens the New Wizard.
6
7. 2. On the Projects tab, select MFC AppWizard (exe).
3. Type a name for your project, such as Hello, in the Project Name field.
4. Click OK. This causes the New Wizard to do two things: create a project directory (specified
in the Location field) and then start the AppWizard.
Steps to create application using AppWizard
1. In Step 1 of the AppWizard, specify that whether you want to create a Dialog-based
application , SDI or MDI application. Click Next at the bottom of the wizard.
7
8. 2. In Step 2 of the AppWizard, the wizard asks you about a number of features that you
can include in your application. Here we check the option for including support for
database files in your application. Because we want to create any database application
,so check this box. Then we have to click on Data Source button to select data source
which we have created.
8
9. 3. In Step 3 of the AppWizard, the wizard asks you about a number of features that you
can include in your application. You can uncheck the option for including support for
ActiveX controls if you will not be using any ActiveX controls in your application.
Because you won't be using any ActiveX controls in today's application, go ahead and
uncheck this box.
9
10. 4. In Step 4 of the AppWizard, the wizard asks you about a number of features that you
can include in your application. You can check or uncheck the features which you do not
want or want in your application. Click Next at the bottom of the wizard to proceed to
the next AppWizard step.
10
11. 5. In Step 5 of the AppWizard, leave the defaults for including source file comments and
using the MFC library as a DLL. Click Next at the bottom of the wizard to proceed to the
final AppWizard step.
11
12. 6. The final step of the AppWizard shows you the C++ classes that the AppWizard will create for
your application. Click Finish to let AppWizard generate your application shell.
Before AppWizard creates your application shell, it presents you with a list of what it is going to
put into the application shell, based on the options you selected when going through the
AppWizard. Click OK and AppWizard generates your application.
DAO vs ODBC
12
13. DAO ODBC
1. Data Access Object Open Data Base Connectivity
2. DAO is an object-oriented ODBC is a call-oriented API .
database programming model.
3. DAO allows you to open a ODBC doesn’t.
database and directly perform data
definition language calls .
4. DAO enforces the notion of When you use ODBC to connect to a
connecting to a database as an database, you're connected direct
object. When you use DAO, you'll to the database and you're ready to
ask it to get connected to the work with its data.
database and use a workspace
object to manipulate the database
itself, i.e. the configuration of the
database and its tables, indexes
and other entities.
5. Speed is relatively faster than Speed is slower
ODBC for jet database engine
databases.
6. DAO is best choice when you ODBC is for various type of
have DAO components installed on database which provides ODBC
your machine and you are using drivers such as SQL Server, Oracle,
Microsoft's jet database engine MS-Access. for connecting to
based databases such as MS-Access, different databases no matter how
SQL Server, MS-Excel or Paradox. big they are - from tiny text files to
large databases.
13
14. 7. DAO can manipulate ODBC ODBC can manipulate ODBC data
databases. But it is less efficient for bases much efficiently.
this task becoz it is best used with
.mdb database files.
8. DAO classes in MFC only two ODBC classes in MFC only
classes CDaoDatabase and represent two classes: CDatabase
CDaoRecordset . and CRecordset.
9. System implements DAO System implements ODBC libraries
libraries as COM objects. as a set of DLLs.
10. DAO best suited for application If your application using other
that manipulate .mdb and other file database now and will using other
formats are FoxPro and Excel. So database in future then using ODBC
application that use Microsoft is beneficial.
Access and always using it ,then
you use DAO.
11. DAO classes provide more But in ODBC , many functions are
powerful set of methods that can not defined in ODBC which are
use to manipulate database defined in DAO .
without having to write a lot of
complicated code or SQL
statements.
13. DAO is only interested in ODBC connects to a data source.
connecting to databases.
14
15. 14. A DAO program would have to The users of an ODBC program can
be written to be configurable get to the Control Panel and
before you could change the reconfigure their data source list to
database you were working with. point at a different database
without any trouble at all. This fact
makes ODBC somewhat more
scaleable and far more manageable
than DAO.
15. DAO is more efficient at ODBC is less efficient at accessing
accessing the native file formats it that same data through the
supports than ODBC appropriate driver.
16. Under DAO, dynamic recordsets Under ODBC, snapshot recordsets
are the default. are the default .
17. DAO to be less efficient than More efficient than DAO.
ODBC when trying to access a
remote server .
Similarities between DAO and ODBC
ODBC and DAO both can manipulate ODBC data sources. However, DAO is less efficient at this
task because it's best used with .mdb database files.
AppWizard can create a basic database application based on either the ODBC or DAO classes.
Which type of application you want to create depends, at least in some part, on the type of
databases with which you will be working.
ODBC and DAO both use objects of an MFC database class to provide a connection to the
database being accessed. In ODBC, this database class is called CDatabase, whereas in DAO, the
class is called CDaoDatabase. Although these classes have different names, the DAO database
class contains some members similar to those found in the ODBC class.
15
16. Similarities between DAO and ODBC
ODBC and DAO both use objects of a recordset class to hold the currently selected records from
the database. In ODBC, this recordset class is called CRecordset, whereas in DAO, the class is
called CDaoRecordset. Although these classes have different names, the DAO recordset class
contains not only almost the same members as the ODBC class but also a large set of additional
member functions.
ODBC and DAO use similar procedures for viewing the contents of a data source. That is, in both
cases, the application must create a database object, create a recordset object, and then call
member functions of the appropriate classes to manipulate the database.
Choosing Between MFC DAO and ODBC Classes
16
17. Can I With DAO classes? With ODBC classes?
Access .MDB files YES YES
Access ODBC data YES YES
sources
Available for 16 Bit YES
Available for 32 Bit YES YES
Available for 64 Bit YES
Database engine Microsoft Jet database Target DBMS
support engine
DDL support YES Only through direct
ODBC calls
DML support YES YES
Nature of the MFC "Wrapper" of DAO core Simplified abstraction
implementation functions rather than a
"wrapper" of the ODBC
API
Optimal for .mdb files (Microsoft Any DBMS for which
Access) you have a driver,
especially in
client/server
situations
17