Connection Object - Pearson Education
Download
Report
Transcript Connection Object - Pearson Education
IT 390 Business Database Administration
Unit 9: Database Access Standards
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 1
Objectives
• Identify and classify ODBC, OLE DB, and ASP
standards.
Introduce database access standards.
Explain ODBC, OLE DB and ASP standards.
• Define and apply basic XML and ADO .NET
• Explain XML and ADO.NET
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 2
ODBC Introduction
• You need interfaces to enable applications
interact and exchange data with databases.
• ODBC is a Microsoft specification for creating a
database application programming interface
(API). The ODBC API is implemented in the form
of drivers.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 3
More about ODBC
• The ODBC works as an interface between an
RDBMS and Application Program. This setup
works in a three-tier architecture.
• SQL Server 2000 connects to development
modules using specific API drivers.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 4
ODBC Architecture
• The components of ODBC architecture
enable the ODBC API to communicate with a
database. These components interact with
each other to make the database accessible
to the user.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 5
ODBC Visual Overview
API
ODBC
SQL
Server
2000
ODBC
API
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 6
Functions of ODBC
• Enables applications to communicate with
different databases
• Facilitates SQL grammar conversion
• Enables a single application to use different
drivers
• Enables applications to interact with different
types of databases
• Allows the use of enhanced DBMS features
• Enables applications to achieve a three-layered
architecture
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 7
Components of ODBC
There are four components of the ODBC architecture that interact
with each other to to make the required output available to the user.
• Application: Applications are the programs that call the ODBC API to
communicate with the DBMS.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 8
Components of ODBC
Driver manager: It is the component that enables
communication between an application and a
driver by monitoring the transactions between
them. A driver manager also performs basic errorhandling tasks after a connection is established
between the application and the driver.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 9
Components of ODBC (cont.)
• Drivers: Drivers are software components that
provide functions in the ODBC API.
• Data source: A data source stores all the
information that is necessary to connect to a
database or a DBMS that is stored on the same
computer as the client computer or on another
computer on the network.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 10
ODBC is NOT Microsoft exclusively
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 11
Setting Up an ODBC Data Source
• You use the ODBC Data Source Administrator
dialog box to create a DSN. The ODBC Data
Source Administrator function is available in
Control Panel in the Windows 98 operating
system. In Windows 2000, the ODBC Data
Source Administrator function is available under
Administrative Tools.
• To set up an ODBC Data Source:
Double-click the Data Sources icon in the Control
Panel.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 12
Setting Up an ODBC Data Source (cont.)
Then, on the ODBC Data Source Administrator screen, click the System DSN tab.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 13
Setting Up an ODBC Data Source (cont.)
In the System DSN tabbed page, click the Add button.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 14
Setting Up an ODBC Data Source (cont.)
Clicking the Add button displays the Create New Data Source dialog box. In
this dialog box, select the SQL Server option, and click the Finish button.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 15
Setting Up an ODBC Data Source (cont.)
The Create a New Data Source to SQL Server wizard appears. In the first
screen of the Create New Data Source Wizard, specify the name for a data
source, a description for the data source, and the name of the SQL Server that
you want the DSN to connect.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 16
Setting Up an ODBC Data Source (cont.)
On the next screen, click the With SQL Server authentication using a
login ID and password entered by the user option. Then, specify a
login ID and password.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 17
Setting Up an ODBC Data Source (cont.)
After specifying the login ID and password for SQL authentication, select the
Change the default database to option, and select the name of the database
from the drop-down list. Accept the default selections for other options, and
then click the Next button.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 18
Setting Up an ODBC Data Source (cont.)
On the next screen of the wizard, accept all the default selections, and then
click the Finish button.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 19
Setting Up an ODBC Data Source (cont.)
You can see the data source name included in the list of other data sources on
the System DSN tabbed page.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 20
Using an ODBC Data Source
To connect to the database, you need a front-end
application. In addition, you need to call the
following functions in the code of the front-end
application.
1. Call the SQLAllocEnv or SQLAllocHandle function
to get an environment handle.
2. Call the SQLAllocConnect function.
3. Call the SQLConnect function.
4. Call the SQLExecDirect function to execute the
SQL statements.
5. Call the SQLFreestmt function to close the
connection.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 21
State whether True or False
• The functions of ODBC are:
1) Enables applications to communicate with
only SQL Server 2000.
2) Enables a single application to use different
drivers.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 22
Solution (True or False)
1) False
2) True
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 23
Class Activity
• Choose the correct ODBC components
from the following list:
Application
File system
Driver manager
Driver
Operating system
Data source
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 24
Solution
•
•
•
•
Application
Driver manager
Driver
Data source
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 25
OLE DB and its Components
• OLE DB consists of three components:
Data providers
Data consumers
Service components
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 26
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 27
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 28
•An ADO object model uses object-oriented
programming techniques to communicate
with the underlying data source. ADO
creates a layer between the applications and
the OLE DB provider.
• The ADO object model consists of nine
objects and four collections.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 29
The ADO Object Model (cont.)
The nine objects are:
• Connection
• Error
• Command
• Recordset
• Record
• Parameter
• Field
• Property
• Stream
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 30
The ADO Object Model (cont.)
The four collections are:
• Fields
• Properties
• Parameters
• Errors
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 31
Accessing Databases by Using ADO
The generic steps to access databases using ADO
are:
1. Set up reference to an ADO Connection object.
2. Define the connection string to be used when
opening the connection object. The connection
string can be defined in two ways, with a DSN or
without a DSN.
3. Open the connection object by using the Open
method of the connection object.
4. Use the state property of the connection object to
check whether or not a connection is established
successfully.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 32
Accessing Databases Using ADO (cont.)
5. Execute SQL statements after the connection is
established.
6. Create a recordset object and execute it by using
the Open method of the recordset object.
7. Close the recordset object. In addition, close the
connection object by calling the Close method of
the connection object.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 33
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.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 34
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.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 35
The ADO Object Model
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 36
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.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 37
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.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 38
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.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 39
ADO Constants:
Isolation Levels
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 40
ADO Constants:
Cursor Levels
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 41
ADO Constants:
Lock Types
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 42
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=Ses
ame”
%>
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 43
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
%>
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 44
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
>%
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 45
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
>%
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 46
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
>%
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 47
ADO Example: Reading a Table
Artist.asp
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 48
ADO Example: Reading a Table
The Artist.asp Results
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 49
Class Activity
• Identify the objects in the ADO Model:
Connection
Error
Recordset
File
Attribute
Stream
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 50
Solution
•
•
•
•
Connection
Error
Recordset
Stream
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 51
Accessing Databases by Using XML
• XML provides a way to define meaningful data
structures and eases the process of data
exchange.
• The advantages of using XML are:
Provides extensible tags to define applicationspecific data structures
Enables the creation of platform-independent
application
Requires only updated document to be uploaded
on the Internet instead of the complete document
Displays the same information in different formats
on the Web
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 52
More about XML
• Provides extensible tags for defining
application-specific data structures.
• Enables the creation of platform-independent
applications.
• Helps reduce the Internet traffic.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 53
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 54
XML Documents
• XML can be used to present any kind of
information in a simple, structured format.
The following are the parts of an XML document:
An XML document begins with a
<?xml version =“1.0”?> declaration tag.
The second component in an XML document is
called an element. An element is referred to as a
root element, when it describes the details about
all other elements.
Below the root element, there is a hierarchy of
elements.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 55
XML Documents (cont.)
All the elements in an XML document contain
some data. The data is called content.
• When you create an XML document, you need to
follow certain rules. These rules are:
Every start tag should have a closing tag
called the end tag.
Tags should not overlap because XML
documents follow a hierarchy.
There can be only one root element.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 56
XML for Data Exchange
• The architecture of an application has three
layers.
The business layer
The data layer
The user interface
• These layers communicate with each other and
exchange data to perform the functions required
in an application.
• In this entire process of data exchange, XML is
used extensively at each stage.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 57
SQL Server 2000 and Relational Data
in XML
• SQL Server 2000 provides the OPENXML
function to insert and update records in the
database from an XML document. The
OPENXML function reads the data from an
XML document and inserts it into the database.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 58
Integrating XML with SQL Server 2000
• SQL server 2000 is an XML-enabled RDBMS that
retrieves data from an SQL database and
presents it as XML data. To retrieve data, you
need to use the FOR XML clause
in the SELECT statement.
• To get the XML output for a SQL query, an
additional clause is added at the end of the SQL
statement:
[FOR XML {RAW|AUTO|EXPLICIT} [, XMLDATA]
[, ELEMENTS] [, BINARY base64]]
• The FOR XML clause has three distinct options:
RAW, AUTO, and EXPLICIT.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 59
Inserting and Updating Relational Data from XML
Data
• SQL Server 2000 provides the OPENXML
function to insert and update records in a
database from an XML document.
• To insert and update records, the OPENXML
function uses two system stored procedures:
sp_xml_preparedocument
sp_xml_removedocument
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 60
Class Activity
• Identify the error in the following XML code
displaying the employee name:
<?xml version ="1.0"?>
<EmployeeInfo>
<Employee ID="001">
<EmployeeName>
Joe
</EmployeeInfo>
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 61
Solution
• The correct code is:
<?xml version ="1.0"?>
<EmployeeInfo>
<Employee ID="001">
<EmployeeName>
Joe
</EmployeeName>
/*the closing tag was missing*/
</EmployeeInfo>
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 62
Rules for Creating an XML Document
• Start tag has a closing tag called the end tag.
• Tags should not overlap because an XML
document follows a hierarchy.
• There can be only one root element.
• XML is a case-sensitive language.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 63
Class Activity
• Identify the various XML formatting
options:
RAW
2) AUTO
3) TEXTSPACE
4) EXPLICIT
5) NAME
1)
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 64
Solution
• RAW
• AUTO
• EXPLICIT
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 65
Summary
• ODBC is a specification for creating a database
API.
• There are four main components of ODBC:
Application
Driver Manager
Driver
Data Source
• You use the ODBC Data Source Administrator
dialog box to create a DSN.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 66
Summary
• OLE DB and ADO provide a common method to
access data from different data sources.
• OLE DB has three components:
Data provider
Data consumers
Service components
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 67
Summary
• The ADO object model consists of the
following nine objects:
Connection
Error
Command
Recordset
Record
Parameter
Field
Property
Stream
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 68
Summary
• The steps to access database using ADO
are:
1.
2.
3.
4.
5.
6.
7.
Set up reference to an ADO Connection
object.
Define the connection string to be used.
Open the connection object.
Use the state property to check the
establishment of the connection.
Execute SQL statements.
Create a recordset object.
Close the recordset object and the connection
object.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 69
Summary
•
•
XML is a standard that can present structured
data in the text format on the Web.
Parts of an XML document:
Declaration statement
Tags
Elements
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 70
Summary
• You use the FOR XML clause in the SELECT
statement to integrate XML with SQL Server.
• The FOR XML clause has three distinct options:
RAW
AUTO
EXPLICIT
• SQL Server 2000 provides an OPENXML function
to insert and update records in a database from
an XML document.
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 71
Summary
Did
you understand the key points from
the Lesson?
Do
you have any questions??
© 2006 ITT Educational Services Inc.
Course Name: IT390 Business Database
Administration Unit 9 Slide 72