Download Kroenke-DBP-e10-PPT-Chapter12

Document related concepts

Microsoft Access wikipedia , lookup

Oracle Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Ingres (database) wikipedia , lookup

Concurrency control wikipedia , lookup

Navitaire Inc v Easyjet Airline Co. and BulletProof Technologies, Inc. wikipedia , lookup

Functional Database Model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

SQL wikipedia , lookup

ContactPoint wikipedia , lookup

PL/SQL wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Database model wikipedia , lookup

Transcript
COS 236
Day 22
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-1
Agenda
• Assignment 9 Posted
– Due Today
• Assignment 10 Posted
– Due April 26
– 1 (2?) more to go
• Last Capstone Progress Reports Due April 26
• Today we will discuss
– OBDC, OLE DB, ADO and ASP
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-2
End of days? (subject to
change)
• April 23
– DP Chap 12
– SQL Chap 11
– Assignment 9 due
• April 26
– DP Chapter 13
– Assignment 10 due
• April 30
– DP Chap 14
• May 3
– DP Chap 15
– Review
– Assignment 11 Due
• May 9
– Quiz 3
– 10 AM
– Capstone presentations
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-3
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter Twelve:
ODBC, OLE DB, ADO,
and ASP
Part One
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-4
Introduction
• Because database applications today reside in a
complicated environment, various standards have been
developed for accessing database servers.
• Some of the important standards are
– OBDC (Open Database Connectivity) is the early standard for
relational databases.
– OLE DB is Microsoft’s object-oriented interface for relational and
other databases.
– ADO (Active Data Objects) is Microsoft’s standard providing
easier access to OLE DB data for the non-object-oriented
programmer.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-5
The Web Server Data Environment
• A Web
server
needs to
publish
applications
that involve
different
data types.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-6
The Role of the ODBC Standard
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-7
The Role of ODE DB
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-8
The Role of ADO
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-9
Open Database Connectivity
(OBDC)
• The Open Database Connectivity (ODBC) standard
provides a DBMS-independent means for processing
relational database data.
• It was developed in the early 1990s by an industry
committee and has been implemented by Microsoft and
many other vendors.
• The goal is to allow a developer to create a single
application that can access databases supported by
different DBMS products without needing to be changed
or recompiled.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-10
ODBC Architecture
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-11
OBDC Components (Continued)
• OBDC consists of a data source, an
application program, a driver manager, and a
DBMS driver.
• A data source is the database and its
associated DBMS, operating system, and
network platform.
– An ODBC data source can be a relational database, a
file server, or a spreadsheet.
• An applications program issues requests to
create a connection with a data source.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-12
OBDC Components
• A driver manager determines the type of DBMS
for a given ODBC data source and loads that
driver in memory.
• A DBMS driver processes ODBC requests and
submits specific SQL statements to a given type
of data source.
– A single-tier driver processes both ODBC calls and
SQL statements.
– A multiple-tier driver processes ODBC calls, but
passes the SQL requests to the database server.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-13
ODBC Driver Types:
ODBC Single-Tier Driver
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-14
ODBC Driver Types:
ODBC Multiple-Tier Driver
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-15
Conformance Levels
• Levels of conformance balance the scope of the
OBDC standard.
• There are two types of conformance levels:
– ODBC conformance levels concern the features and
functions that are made available through the driver’s
application program interface (API).
• A driver API is a set of functions that the application can call
to receive services.
– SQL conformance levels specify which SQL
statements, expressions, and data types a driver can
process.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-16
Summary of
OBDC Conformance Levels
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-17
Summary of
OBDC Conformance Levels
(Continued)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-18
Summary of
SQL Conformance Levels
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-19
Summary of
SQL Conformance Levels
(Continued)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-20
ODBC Data Sources
• A data source is an ODBC data structure that identifies
a database and the DBMS that processes it.
• Three types of data source names:
– A file data source is a file that can be shared among database
users having the same DBMS driver and privilege.
– A system data source is local to a single computer and may be
used by the operating system and any user on that system.
• System data sources are recommended for Web servers.
• To define a system data source name, the type of driver and the
database need to be specified.
– A user data source is available only to the user
who created it.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-21
Creating a System Data Source:
Selecting the Oracle Driver
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-22
Creating a System Data Source:
Setting Data Source Properties
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-23
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter Twelve Part One
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-24
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter Twelve:
ODBC, OLE DB, ADO,
and ASP
Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-25
OLE DB
• OLE DB is an implementation of the Microsoft
OLE object standard.
– OLE DB objects are COM objects and support all required
interfaces for such objects.
• OLE DB breaks the features and functions of a DBMS
into COM objects, making it easier for vendors to
implement portions of functionality.
– This characteristic overcomes a major disadvantage of ODBC.
– With ODBC, a vendor must create an ODBC driver for almost all
DBMS features and functions in order to participate in ODBC at
all.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-26
Object-Oriented Concepts
• An object-oriented programming object is an
abstraction that is defined by its properties and
methods.
– An abstraction is a generalization of something.
– A property specifies set of characteristics of an
object.
– A method refers to actions that an object can
perform.
– A collection is an object that contains a group of
other objects.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-27
OLE DB Goals
• Create object interfaces for DBMS functionality pieces:
– Query, update, transaction management, etc.
• Increase flexibility:
–
–
–
–
Allow data consumers to use only the objects they need.
Allow data providers to expose pieces of DBMS functionality.
Providers can deliver functionality in multiple interfaces.
Interfaces are standardized and extensible.
• Provide object interfaces over any type of data:
– Relational and non-relational database, ODBC or native, VSAM
and other files, Email, etc.
• Do not force data to be converted or moved from where
it is.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-28
OLE DB Basic Constructs
• There are data consumers and data providers:
– Data consumers - Users of OLE DB functionality.
– Data providers - Sources of OLE DB functionality.
• An interface is a set of objects and the properties and
methods they expose in that interface:
– Objects may expose different properties and methods in different
interfaces.
• An implementation is how an object accomplishes its
tasks:
– Implementations are hidden from the outside world and may be
changed without impacting the users of the objects.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-29
OLE DB Terminology:
Data Providers
• A rowset is equivalent to a cursor.
• OLE DB has two types of data providers:
– Tabular data provider — exposes data via rowsets.
• Examples: DBMS, spreadsheets, ISAMs, email.
– Service provider — a transformer of data through
OLE DB interfaces.
• It is both a consumer and a provider of transformed data.
• Examples: query processors, XML document creator.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-30
Rowset Interfaces
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-31
Active Data Objects (ADO)
• Active Data Objects (ADO) characteristics:
– A simple object model for OLE DB data consumers
– It can be used from VBScript, JScript, Visual Basic,
Java, C#, C++
– It is a single Microsoft data access standard
– Data access objects are the same for all types of OLE
DB data
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-32
Invoking ADO from Active Server Pages
• In Microsoft’s Active Server Pages (ASP)
are Web pages where:
– Statements are enclosed within the
characters <% . . .%>.
– ASP statements are processed on the Web
server.
– Other (HTML) statements are processed by
the client Web browser.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-33
The ADO Object Model
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-34
Connection Object
• A connection object establishes a connection
to a data provider and data source.
– Connections have an isolation mode.
• Once a connection is created, it can be used to
create RecordSet and Command objects.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-35
RecordSet Objects
• RecordSet objects represent cursors:
– They have both CursorType and LockType properties.
– RecordSets can be created with SQL statements.
– The Fields collection of a RecordSet can be
processed to individually manipulate fields.
– The Errors collection contains one or more error
messages that result from an ADO operation.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-36
Command Object
• The command object is used to execute
stored parameterized queries or stored
procedures:
– Input data can be sent to the correct ASP
using the HTML FORM tag.
– Table updates are made using the RecordSet
Update method.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-37
ADO Constants:
Isolation Levels
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-38
ADO Constants:
Cursor Levels
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-39
ADO Constants:
Lock Types
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-40
Connection Object:
ASP Code
<%
Dim objConn
Set objConn = Server.CreateObject (“ADODB.connection”)
objConn.IsolationLevel = adXactReadCommitted
‘ use ADOVBS
objConn.Open “ViewRidgeSS”,
%>
<!--#include virtual =“ADOExamples/ADOVBS.inc -->
<%
objConn.Open “DSN=ViewRidgeOracle2;UID=DK1;PWD=Sesame”
%>
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-41
RecordSet Object:
ASP Code
<%
Dim objRecordSet, varSql
varSQL = “SELECT * FROM ARTIST”
Set objRecordSet = Server.CreateObject(“ADODB.Recordset”)
objRecordSet.CursorTye = adOpenStatic
objRecordSet.LockType = adLockReadOnly
objRecordSet.Open varSQL, objConn
%>
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-42
Fields Collection:
ASP Code
<%
Dim varI, varNumCols, objField
varNumCols = objRecordSet.Fields.Count
For varI = 0 to varNumCols - 1
Set objField = objRecordSet.Fields(varI)
‘ objField.Name now has the name of the field
‘ objField.Value now has the value of the field
‘ can do something with them here
Next
>%
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-43
Errors Collection:
ASP Code
<%
Dim varI, varErrorCount, objError
On Error Resume Next
varErrorCount = objConn.Errors.Count
If varErrorCount > 0 Then
For varI = 0 to varErrorCount - 1
Set objError = objConn.Errors(varI)
‘ objError.Description contains
‘ a description of the error
Next
End If
>%
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-44
Command Object:
ASP Code
<%
Dim
objCommand, objParam, objRs
‘Create the Command object, connect it to objConn and
set its format
Set objCommand = Server.CreateObject(“ADODB.command”)
Set objCommand.ActiveConnection = objConn
objCommand.CommandText=“{call FindArtist (?)}”
‘Set up the parameter with the necessary value
Set objParam = objCommand.CreateParameter (“Nationality”,
adChar, adParamInput, 25)
objCommand.Parameters.Append objParam
objParam.Value = “Spanish”
‘Fire the Stored Proc
Set objRs = objCommand.Execute
>%
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-45
ADO
Example:
Reading a
Table
Artist.asp
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-46
ADO Example: Reading a Table
The Artist.asp Results
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-47
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter Twelve Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
12-48
CHAPTER 11:
EMBEDDED SQL
Copyright 2004 Prentice Hall, Inc.
11-49
EMBEDDED SQL
•
•
Structured Query Language can be used in
conjunction with many different procedural and
object-oriented programming host languages.
This approach to programming is termed
embedded SQL, and simply means that the host
language includes the ability to use SQL
statements to both retrieve and store records in a
non-procedural fashion.
Copyright 2004 Prentice Hall, Inc.
11-50
Embedding SQL in Visual Basic 6.0
•
•
•
Database processing is primarily accomplished
through use of the ActiveX Data Objects (ADO)
control.
This specialized control enables a computer
programmer to connect an application program
written in Visual Basic to an existing database.
It is easy to create the database connection.
Copyright 2004 Prentice Hall, Inc.
11-51
Figure 11.1 shows an example employee processing form
with an ADO control.
Copyright 2004 Prentice Hall, Inc.
11-52
ADO Control
•
•
•
•
•
The ADO control is used to process employee records.
The employee processing form displays a subset of the
data columns from the employee table.
The ADO control provides First, Previous, Next, and
Last buttons (arrows) to make it easy to navigate from
one row of data to the next.
When one of the ADO control navigation buttons is
clicked, the form display updates to a new record.
The ADO control also has properties that can be
visually programmed to make a database connection.
Copyright 2004 Prentice Hall, Inc.
11-53
ADO Control
•
•
•
The ConnectionString property stores a character string that
specifies how to connect to a database.
Part of the ConnectionString information specifies where the
database is located.
The ConnectionString also specifies the type of database as
well as information about provider software that is used to
make a database connection. In the coding segment shown
in the following VB6 Example, a connection is made to an
Oracle database named Company.
•
•
REM VB6 Example
ConnectionString = "Provider=MSDAORA.1; User ID=dbock;
Password=mypassword; Data Source=Company; Persist
Security Info=True"
(Note: All of the above code will be entered on a single coding line.)
Copyright 2004 Prentice Hall, Inc.
11-54
ADO Control
•
•
•
•
The CommandType property specifies how data will be
retrieved from a database.
Data rows are retrieved from disk and stored in a
memory object called a recordset.
The RecordSource property stores the actual SQL
statement used to create a recordset.
The records displayed for the recordset in Figure 11.1
were retrieved with the SELECT statement shown in
VB6 Example 11.2.
•
•
REM VB6 Example 11.2
SELECT emp_last_name, emp_first_name, emp_middle_name, emp_ssn,
emp_address, emp_city, emp_state, emp_zip
FROM employee
WHERE emp_zip = '62025';
Copyright 2004 Prentice Hall, Inc.
11-55
ADO Control
•
•
There are two additional properties for each textbox control
that must be set in order for a Visual Basic form to display
database data.
These are the DataField and DataSource properties shown
in Figure 11.2 that displays a Visual Basic property
window that is associated with the textbox control that
displays the employee first name.
Copyright 2004 Prentice Hall, Inc.
11-56
ADO Control
•
•
•
•
•
The DataSource property of each textbox is set to the name
of the ADO control.
Here the ADO control is named adoEmployee.
Setting the DataSource property of a textbox links the
textbox control to the ADO control.
DataField property is set to the appropriate column name
from the employee table in order to specify the exact value
to display in a textbox.
Each textbox on the employee processing form will have the
same value for the DataSource property, but the DataField
property for each textbox will reflect the column of data to
be displayed in that particular textbox.
Copyright 2004 Prentice Hall, Inc.
11-57
Selecting Data Specified by a User Inputted Value
•
•
•
This is achieved by storing the value entered into the
InputBox to a Visual Basic memory variable that stores
string or character data.
The Visual Basic code to produce the InputBox and
store the value is shown in VB6 Example 11.4.
The value entered for the social security number is
stored to the strSSN variable.
•
•
REM VB6 SQL Example 11.4
Dim strSSN As String strSSN = InputBox("Enter Employee SSN:", _
"Employee SSN Search", vbOKCancel)
Copyright 2004 Prentice Hall, Inc.
11-58
Selecting Data Specified by a User Inputted Value
•
•
The program stores the SELECT statement to a second
string variable named strSQL in the code segment
shown in VB6 Example 11.5.
Each piece of the SELECT statement is a string of
characters that are concatenated together with the Visual
Basic concatenation operator (the ampersand – &). The
WHERE clause has the emp_ssn column name set equal
to the value stored to the strSSN variable that was
captured through use of the InputBox.
Copyright 2004 Prentice Hall, Inc.
11-59
Selecting Data Specified by a User Inputted Value
•
At execution time, the SELECT statement stored to the
strSQL variable includes the social security number stored
to the strSSN variable.
•
•
•
REM VB6 Example 11.5
'Store SQL statement to a string variable
strSQL = "SELECT emp_last_name, emp_first_name, " & _
"emp_middle_name, emp_ssn, emp_address, " & _
"emp_city, emp_state, emp_zip" & _
"FROM employee" & _
"WHERE emp_ssn = " & strSSN
Copyright 2004 Prentice Hall, Inc.
11-60
Selecting Data Specified by a User Inputted Value
•
•
The RecordSource property of the ADO control is
updated by storing the strSQL string variable to the
RecordSource property as shown in VB6 Example 11.6.
The ADO control is refreshed with the Refresh method.
The Refresh method automatically creates a new
recordset, and the correct employee row will be
retrieved and displayed on the employee processing
form.
•
•
REM VB6 Example 11.6
'Update the recordset retrieved by the ADO control
adoEmployee.RecordSource = strSQL
adoEmployee.Refresh
http://www.johnsmiley.com/cis18/Smiley017.pdf
Copyright 2004 Prentice Hall, Inc.
11-61
Embedding SQL in Visual Basic.net
•
Visual Basic 6.0 uses the ActiveX Data Objects (ADO)
approach to connect to databases through OLEDB
providers (software drivers).
ADO has evolved into ADO.NET in Visual Basic.NET.
ADO.NET still uses OLEDB providers to connect to
databases such as Oracle, Microsoft SQL Server, and
Microsoft Access.
The major tasks facing a database programmer are:
•
•
–
–
–
Connecting to a database.
Executing SQL statements to add, delete, modify, or retrieve
table rows.
Working with datasets. Datasets replace the recordsets used in
VB 6.0.
Copyright 2004 Prentice Hall, Inc.
11-62
Making a Database Connection
•
A sample VB.NET connection string for an Oracle
database named Company is shown in VB.NET
Example 11.1.
•
•
•
REM VB.NET Example 11.1
strConnection = "Provider=MSDAORA.1; User ID=dbock;
Password=mypassword; Data Source=Company; Persist
Security Info=True"
The connection string value is stored to a string constant
named strConnection. VB.NET Example 11.2 shows
the declaration of a connection object named
objConnection.
Copyright 2004 Prentice Hall, Inc.
11-63
Making a Database Connection
•
The value of the strConnection constant is passed to the
objConnection object as an argument inside
parentheses. Following this the database connection is
opened by using the Open method.
•
•
•
•
•
•
•
•
•
•
•
REM VB.NET Example 11.2
Module MyConnection
Private Const strConnection As String = & _
"Provider = MSDAORA.1; User ID=dbock;
Password=mypassword; & _
"Data Source=Company; Persist Security Info=False"
Sub Main()
Dim objConnection As New _
System.Data.OleDb.OleDbConnection(strConnection)
objConnection.Open()
End Sub
End Module
Copyright 2004 Prentice Hall, Inc.
11-64
Executing SQL Statements
•
•
•
Visual Basic.NET uses a command object
(OleDBCommand) to execute SQL statements.
The approach is again similar to that used in Visual
Basic 6.0 in that a programmer creates a string memory
variable to store the SQL statement to be executed.
The SELECT statement is shown in VB.NET Example
11.3.
•
•
REM VB.NET Example 11.3
SELECT emp_last_name, emp_first_name,
emp_middle_name, emp_ssn,
emp_address, emp_city, emp_state, emp_zip
FROM employee
WHERE emp_zip = '62025';
Copyright 2004 Prentice Hall, Inc.
11-65
Executing SQL Statements
•
•
•
•
•
The .NET code module named MyConnection shown
earlier in VB.NET Example 11.2 needs to be modified
to accommodate reading data.
The modified code is shown in VB.NET Example 11.4.
Note that the strSQL memory variable stores the
SELECT command as a series of concatenated character
strings.
A command object named objCommand is created and
the value of strSQL and the objConnection connection
object are passed to the objCommand object as
parameters inside parentheses.
A reader object (objReader) is created to store the actual
rows returned when the ExecuteReader method of the
objCommand object executes.
Copyright 2004 Prentice Hall, Inc.
11-66
Executing SQL Statements
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
REM VB.NET Example 11.4
Module MyConnection
Private Const strConnection As String = & _
"Provider = MSDAORA.1; User ID=dbock; Password=mypassword; & _
"Data Source=Company; Persist Security Info=False"
Sub Main()
Dim strSQL As String
strSQL = "SELECT emp_last_name, emp_first_name, " & _
"emp_middle_name, emp_ssn, emp_address, " & _
"emp_city, emp_state, emp_zip " & _
"FROM employee " & _
"WHERE emp_zip = '62025';"
Dim objConnection As New _
System.Data.OleDb.OleDbConnection(strConnection)
Dim objCommand As New _
System.Data.OleDb.OleDbCommand(strSQL, objConnection)
Dim objReader As _
System.Data.OleDB.OleDBReader
objConnection.Open()
objReader = objCommand.ExecuteReader()
'Additional code goes here to process the rows returned
'by the SQL query.
End Sub
Copyright 2004 Prentice Hall, Inc.
11-67
End Module
Embedding SQL in COBOL
Data Division Modifications
•
•
•
The Data Division of a COBOL program is used to
declare both the structure of files or databases that a
program will access.
It is also used to declare memory variables known as
Working-Storage variables.
The Working-Storage Section of a COBOL program is
also used to declare tables in memory that will store
data rows that are retrieved from database tables.
Copyright 2004 Prentice Hall, Inc.
11-68
Embedding SQL in COBOL
•
•
•
•
•
EXEC SQL and END-EXEC commands are used to
mark the beginning and end of SQL statements so that a
COBOL compiler can properly compile the program.
The code in COBOL Example 11.1 shows the
declaration of a table named employee that will store
data rows retrieved from the employee table of our
Company database.
This code is located in the Working-Storage Section.
You will also note that only the table columns that will
be retrieved for processing need to be defined in the
table.
The other columns in the employee table are ignored.
Copyright 2004 Prentice Hall, Inc.
11-69
Embedding SQL in COBOL
–
–
–
–
–
–
–
–
–
–
–
–
•
•
* COBOL Example 11.1
EXEC SQL
DECLARE EMPLOYEE TABLE
(EMP_SSN
CHAR(9),
EMP_LAST_NAME
CHAR(25),
EMP_FIRST_NAME
CHAR(25),
EMP_MIDDLE_NAME
CHAR(25),
EMP_ADDRESS
CHAR(50),
EMP_CITY
CHAR(25),
EMP_STATE
CHAR(2),
EMP_ZIP
CHAR(9) )
END-EXEC.
In order to process the employee table, rows that are
retrieved are stored to standard COBOL variables.
These are also declared in the Working-Storage Section.
Copyright 2004 Prentice Hall, Inc.
11-70
Embedding SQL in COBOL
•
•
•
In order to process the employee table, rows that are retrieved are
stored to standard COBOL variables.
These are also declared in the Working-Storage Section.
Row data are moved from the employee table to the COBOL
variables. The Working-Storage declaration of these variables is
shown in COBOL Example 11.2.
–
–
–
–
–
–
–
–
–
–
* COBOL Example 11.2
01 EMPLOYEE-WORK.
05 EMP-SSN
PIC X(9).
05 EMP-LAST-NAME
PIC X(25).
05 EMP-FIRST-NAME
PIC X(25).
05 EMP-MIDDLE-NAME PIC X(25).
05 EMP-ADDRESS
PIC X(50).
05 EMP-CITY
PIC X(25).
05 EMP-STATE
PIC X(2).
05 EMP-ZIP
PICPrentice
X(9). Hall, Inc.
Copyright 2004
11-71
Embedding SQL in COBOL
•
•
•
•
In order to process SQL statements and the
employee_data table declared in COBOL Example 11.2,
a SQL communications area (SQLCA) must be defined
in the server's memory.
The SQL communications area includes a SQLCODE
parameter. The SQLCODE parameter stores coded
values that represent what occurs within an information
system whenever any SQL statement executes.
The normal execution of an SQL statement causes the
SQLCODE parameter to store a value of zero.
Program code must be written for the COBOL program
Procedure Division in order to test the value of
Copyright 2004 Prentice Hall, Inc.
11-72
Contd.
Embedding SQL in COBOL
•
•
SQLCODE through the use of IF statements.
The IF statements will determine if a given SQL
statement executes successfully.
The SQL communications area is created by the
INCLUDE command shown in COBOL example 11.3.
•
•
•
•
* COBOL Example 11.3
EXEC SQL
INCLUDE SQLCA
END-EXEC.
Copyright 2004 Prentice Hall, Inc.
11-73
Embedding SQL in COBOL
Procedure Division Modifications
• The retrieval of data rows through use of an embedded
SQL SELECT statement in COBOL requires the storage
of data values to host variables.
• For the employee table, this simply means that data row
values will be stored to the 01 EMPLOYEE-WORK
memory variable created in COBOL Example 11.2.
• A SELECT statement embedded in a Procedure
Division looks like the one shown in COBOL Example
11.4.
• Note that the host variables are preceded by a colon ( : )
symbol that is used to differentiate these variables to the
program compiler.
Copyright 2004 Prentice Hall, Inc.
11-74
Embedding SQL in COBOL
•
•
The SELECT statement is NOT ended by a semicolon
when it is embedded in COBOL.
The code is located within a paragraph named
PROCESS-EMPLOYEE-DATA.
–
–
–
–
–
–
–
–
–
–
–
–
* COBOL Example 11.4
PROCESS-EMPLOYEE-DATA.
EXEC SQL
SELECT EMP_SSN, EMP_LAST_NAME, EMP_FIRST_NAME,
EMP_MIDDLE_NAME, EMP_ADDRESS,
EMP_CITY, EMP_STATE, EMP_ZIP
INTO :EMP-SSN, :EMP-LAST-NAME, :EMP-FIRST-NAME,
:EMP-MIDDLE-NAME, :EMP-ADDRESS, :EMP-CITY,
:EMP-STATE, :EMP-ZIP
FROM EMPLOYEE
WHERE EMP_ZIP = "62025"
END-EXEC.
Contd.
Copyright 2004 Prentice Hall, Inc.
11-75
Embedding SQL in COBOL
–
–
–
–
–
–
•
•
IF SQLCODE = 0
* (Place additional code here to process the employee row)
ELSE
*
(Insert additional code here to handle the retrieval
*
error – no employees with the specified Zip Code)
END IF.
Additional code would be added to process the data
rows retrieved from the employee table by calling the
PROCESS-EMPLOYEE-DATA paragraph through use
of a standard PERFORM UNTIL command within
COBOL.
The embedded SQL command replaces the use of a
COBOL READ command for the simple retrieval of
data.
Copyright 2004 Prentice Hall, Inc.
11-76
Embedding SQL in COBOL
•
•
•
•
Data can be inserted into the employee table by using
the SQL INSERT statement in place of the COBOL
WRITE command as shown in COBOL Example 11.5.
Let's assume that the data values to be inserted have
already been moved to the work variables defined
within the 01 EMPLOYEE-WORK memory variable
created earlier.
This could be accomplished by using a series of
ACCEPT commands to enable data entry from the
keyboard.
Alternatively, the data values could be READ from a
sequential or indexed data file.
Copyright 2004 Prentice Hall, Inc.
11-77
Embedding SQL in COBOL
•
•
•
•
•
•
•
•
•
•
* COBOL Example 11.5
EXEC SQL
INSERT INTO EMPLOYEE (EMP_SSN, EMP_LAST_NAME,
EMP_FIRST_NAME, EMP_MIDDLE_NAME,
EMP_ADDRESS,
EMP_CITY, EMP_STATE, EMP_ZIP)
VALUES (:EMP-SSN, :EMP-LAST-NAME, :EMP-FIRSTNAME,
:EMP-MIDDLE-NAME, :EMP-ADDRESS, :EMP-CITY,
:EMP-STATE, :EMP-ZIP)
END-EXEC.
Note that the values inserted from the Working-Storage
variables match up on a one-for-one basis with the
column names defined for the employee table in the
Working-Storage Copyright
Section
of the
2004 Prentice
Hall,program.
Inc.
11-78