Chapter06 PDF
Chapter06 PDF
Chapter06 PDF
qc
7/31/00
09:43
Page 89
C H A P T E R
Accessing
Databases from
Visual Basic
Microsoft Database
Programming APIs
Just because your database server is based on SQL doesnt
mean that you can easily access it from your favorite programming language. Many database vendors provide a special precompiler that translates embedded database statements into
database subroutine calls that in turn communicate with the
database server. The problem with this approach is that you
need a different pre-compiler for each programming language
the database vendor supports. Of course, since the pre-compilers are specific for each database server, youll need a pre-compiler for each database server for which you develop programs.
Rather than developing a large number of pre-compilers for
each combination of database server and programming language, Microsoft developed a standard called Open Database
Connectivity (ODBC), which later evolved into OLE DB.
In This Chapter
ODBC and OLE DB
DAO object model
RDO object model
ADO object model
Visual Basic
database tools
4728-3 ch06.f.qc
90
7/31/00
09:43
Page 90
ODBC
The ODBC standard defines an Application Programming Interface (API) for database
programming. This allows you to write a program using standard subroutine calls
for any database server that supports ODBC, making it possible for the same object
code to access any ODBC-compatible database server.
ODBC architecture
ODBC is based on the idea that the calls to the API routines made by the application program are translated to lower calls that are passed onto a database driver
(see Figure 6-1). The database driver in turn performs the necessary work to talk to
the database server. Thus, the database vendor need only provide an ODBC-compatible driver for each client computer system and not for each compiler on each
client operating system.
Client Computer
Application
Program
ODBC API
ODBC Database
Driver
Database Server
Database Engine
ODBC Interface
Figure 6-1: The client side of the ODBC architecture is based on the driver model.
On the database server side, all the database vendor needs to do is provide a single
ODBC interface, which will be shared by all ODBC clients. This means that database
vendors can preserve their native interface, plus any other specialized interfaces
for other applications.
4728-3 ch06.f.qc
7/31/00
09:43
Page 91
A utility program called the ODBC Administrator is included as part of the operating system to manage the set of ODBC drivers and database servers that are available to the ODBC API. This program allows you to add and remove ODBC drivers,
specify how to connect to the database server, and include security information
that will be used when the connection between the client computer and the
database server is opened.
Drawbacks to ODBC
While the architecture of ODBC allows a great deal of flexibility on the part of the
database vendor, there are several drawbacks to writing ODBC applications. First,
the ODBC APIs are difficult to use, especially if you arent programming in C. Second,
the ODBC APIs are often slower than the native database interface. Third, the ODBC
API often imposes restrictions on the SQL statements that can be used.
While the first drawback applies to all database servers, the second two drawbacks
apply mainly to non-Microsoft database servers. Microsoft uses ODBC as the native
interface to both the Jet database and SQL Server. They spent a lot of time tuning
the interface for optimal results. While other database vendors support ODBC, their
native interfaces may offer improved performance and functionality, especially for
non-Microsoft compilers.
Visual Basic 6 and Access 2000: While Jet 3.51 is shipped with Visual Basic 6, Jet
4.0 is shipped with Access 2000. While its possible to use Visual Basic 6 and DAO
with Jet 4.0, many of the new features found in Jet 4.0 can only be used if youre
using ADO.
91
4728-3 ch06.f.qc
92
7/31/00
09:43
Page 92
OLE DB
After years of working with ODBC, Microsoft recognized that the database server to
database client model used by ODBC could be generalized as a data provider to data
consumer model (see Figure 6-2).
Data Request
Data Provider
Data Customer
Data Response
Data providers
A data provider is a program that supplies data to another program. In the ODBC
model, the data provider is the database server. However, in the OLE DB, nearly
anything that can produce data could be considered a data provider. This allows
programmers to treat such things as an Excel workbook, a flat file, or another custom-built program like a database server.
4728-3 ch06.f.qc
7/31/00
09:43
Page 93
Data consumers
A data consumer is simply a client program that requests data from a data provider.
Two types of information may be requested from the data producer: data and metadata. Data is the information used by an application, while metadata is information
about the structures used to hold the data. Metadata includes such information as
the name of each column returned, its size and data type, and other descriptive
information.
OLE DB providers
Table 6-1 lists the OLE DB providers that are available with Visual Basic 6. Other
OLE DB providers may be available from your specific database vendor.
93
4728-3 ch06.f.qc
94
7/31/00
09:43
Page 94
Table 6-1
Common OLE DB Providers
Provider Name
Description
Even if your database server doesnt support OLE DB, that doesnt mean you cant
access the database from ADO. There is a special provider known OLE DB Provider
for ODBC. This allows you to connect your ADO-based application to any ODBC
database. Of course, you should try to use a native OLE DB provider whenever
possible.
4728-3 ch06.f.qc
7/31/00
09:43
Page 95
Database Designer
SQL Editor
T-SQL Debugger
Query Designer
Data Report Designer
UserConnection Designer
With the exception of the UserConnection Designer, these tools will work only in an
ADO programming environment. The UserConnection Designer works only with
RDO objects.
You can use the Data Environment Designer to perform the following tasks:
Define Connection objects to your database using either OLE DB data sources
or ODBC data sources.
Define Command objects using tables, queries, and stored procedures from
your database.
Create hierarchies of Command objects that can be used with hierarchical
tools like the HFlexGrid control and the Data Reporter Designer.
95
4728-3 ch06.f.qc
96
7/31/00
09:43
Page 96
Drag and drop fields and tables from a Command object in the designer onto
a Form object or the Data Reporter designer that are automatically bound to
the Command object.
Specify default the type of control to be used as part of drag and drop operations.
Bind data-aware controls to Field objects within a Command object.
Attach code for Connection and Recordset objects in the Data Environment
Designer.
Trap all ADO events for the Connection and Command objects.
The Visual Database Tools are present only in the Enterprise Edition of Visual Basic.
This not only includes the Data View Window, but the Database Designer, the
Query Designer, the SQL Editor, and the T-SQL Debugger.
You can use the Data View Window to perform the following tasks:
Create a Connection object that can be used to access your database while in
design mode or at runtime.
Design your database graphically using the Database Designer.
4728-3 ch06.f.qc
7/31/00
09:43
Page 97
Database Designer
The Database Designer is a graphical tool that presents a database using a graphical diagram (see Figure 6-5). The Database Designer works with both Microsoft SQL
Server and Oracle database systems.
You can use the Database Designer to perform the following tasks:
Create and modify tables.
Add and delete indexes.
Define relationships between tables.
Save SQL scripts to create or update your database structures.
97
4728-3 ch06.f.qc
98
7/31/00
09:43
Page 98
SQL Editor
The SQL Editor provides a simple editor to help you write stored procedures (see
Figure 6-6).
Figure 6-6: Editing a simple stored procedure using the SQL Editor
You can use the SQL Editor to perform the following tasks:
Create and edit SQL stored procedures.
Execute stored procedures.
Use the T-SQL Debugger to debug your stored procedures.
T-SQL Debugger
The T-SQL Debugger helps you test and debug your stored procedures (see
Figure 6-7).
You can use the T-SQL Debugger to perform the following tasks:
Display the contents of local variables and parameters.
Modify local variables and parameters while executing the stored procedure.
Control execution by using breakpoints.
Step through the stored procedure.
View global variables.
View the call stack.
4728-3 ch06.f.qc
7/31/00
09:43
Page 99
Figure 6-7: Testing a simple stored procedure using the T-SQL Debugger
Query Designer
The Query Designer is a tool that works with the Data View Window to drag and
drop tables and columns to create a view or query (see Figure 6-8).
You can use the Query Designer to perform the following tasks:
Drag and drop tables from the Data View Window onto your query.
Use a criteria grid to specify search criteria and sort order.
99
4728-3 ch06.f.qc
100
7/31/00
09:43
Page 100
You can use the Data Reporter Designer to perform the following tasks:
Create reports by dragging and dropping fields from the Data View Window.
Add controls to your report, just like you add controls to a Form in a Visual
Basic program.
Implement a Print Preview function in your program to allow users to preview
the report before sending it to the printer.
Allow the user to resume working while the report is running (i.e., asynchronous execution of the report).
UserConnection Designer
The UserConnection Designer is a tool that assists the RDO programmer in building
connection (rdoConnection) and query (rdoQuery) objects. These objects are made
available at the project level and can be used anywhere in your program. A key part
of these objects is that they have a simplified method for responding to database
events. These objects also make it easier to call stored procedures at runtime.
Caution
RDO not ADO: The UserConnection Designer supports only the RDO object
model. Do not try to use it with DAO or ADO object models. The Data Environment
Designer is a much improved version of the UserConnection Designer and is available for use with ADO programs.
4728-3 ch06.f.qc
7/31/00
09:43
Page 101
Summary
In this chapter you learned that:
ODBC was originally developed as an alternative to developing pre-compilers
for each programming language to translate SQL statements into executable
code.
DAO was created to provide an object-oriented API to access Microsoft Jet as
well as any ODBC database.
RDO is a low-overhead, general-purpose ODBC interface.
OLE DB is a general-purpose interface that allows data consumers to talk to
data producers.
101
4728-3 ch06.f.qc
102
7/31/00
09:43
Page 102
ADO is the recommended way for Visual Basic programmers to access OLE DB.
Visual Basic includes a number of specialized tools for the database programmer, which include the Data Environment Designer, Data View Window, Database Designer, SQL Editor, T-SQL Debugger, Query Designer, Data Reporter
Designer, and the UserConnection Designer.