Adodb: Harold R. Lucero, Mit
Adodb: Harold R. Lucero, Mit
HAROLD R. LUCERO,MIT
ITEP004
ADODB
ADO is a Microsoft technology
ADO stands for ActiveX Data Objects
ADO is a Microsoft Active-X component
ADO is a programming interface to access data in a database
ADO is more flexible
Using ADO is quite simple and allows programmers to provide flexible
database front ends to users that are reliable and include many features
As a VB6 programmer ADO is important to know as most commercial
VB programming exercises involve providing a front end to some sort of
database.
DATABASE MANAGEMENT SYSTEM
ITEP004
ADODB OBJECTS
Connection
Command
Recordset
ITEP004
CONNECTION OBJECT
oThis object represents an open connection to the data source.
o This connection can be a local connection or can be across a
network in a client server application.
o Some of the methods and properties of this object are not available
depending on the type of data source connected to.
ITEP004
CONNECTION OBJECT KEY PROPERTIES
ConnectionString (String)
Defines in string form the location of the data source you wish to connect to.
Key fields in the string you will use are the "Provider=" and the "Data
Source=" fields. You may also use the "Mode=" field.
Example :
Data Source=c:\test.mdb;Mode=Read|Write;
driver=(SQL Server)
server=bigsmile;uid=sa;pwd=pwd;database=pubs
ITEP004
CONNECTION OBJECT KEY PROPERTIES
Provider (String)
A string defining the provider of a connection object. An example follows:
Provider=Microsoft.Jet.OLEDB.4.0
This string connects to a MS Jet 4.0 compliant database (an Access DB
for example.)
ITEP004
CONNECTION OBJECT KEY PROPERTIES
Mode (connectModeEnum)
Sets (or returns) the permissions for modifying data across the open
connection. Available permissions include Read, Write, Read/Write, and
various Share/Deny types.
CursorLocation (cursorLocationEnum)
Sets the location of the cursor engine. You can select client side or server
side, for simpler databases (like Access) client side is the only choice.
ITEP004
CONNECTION OBJECT KEY METHODS
CLOSE
Closes
an open connection
OPEN
Opens a connection with the settings in the
ConnectionString property.
ITEP004
COMMAND OBJECT
ITEP004
COMMAND OBJECT KEY PROPERTIES
ACTIVECONNECTION (dbCon as ADODB.Connection)
Defines the Connection object the command belongs to.
COMMANDTEXT (String )
Contains the text of the command you want to execute against a data source.
This can be a table name, a valid SQL string, or the name of a stored procedure
in the data source. Which one you use is determined by the CommandType
property.
ITEP004
COMMAND OBJECT KEY METHODS
EXECUTE
Executes the query, SQL statement, or stored procedure
specified in the CommandText property and returns a
RecordSet object.
ITEP004
RECORDSET OBJECT
The RecordSet object represents a complete set of records
from an executed command or from an underlying base table
in the database.
A key thing to note is that a RecordSet object references
only one record at a time as the current record.
ITEP004
RECORDSET OBJECT KEY PROPERTIES
CURSORLOCATION (CursorLocationEnum)
This sets or returns the location of the cursor engine for the database. The
options are client side and server side, for less advanced databases like Access
you may only be able to select client side.
CURSORTYPE (CursorTypeEnum )
Sets the cursor type. CursorType typically determines when and to whom
database changes are immediately visible to. For client side cursor locations only
one type of CursorType is available, adOpenStatic.
ITEP004
RECORDSET OBJECT KEY PROPERTIES
EOF and BOF (Boolean )
End Of File and Beginning Of File. When at the first record of an open RecordSet
BOF will be true, when at the last EOF will be true. If you ever return a RecordSet
with no records then EOF and BOF will be true. This provides an ideal way of
testing if any records have been returned by a query.
FIELDS (Collection )
Returns a collection of the field objects for an open record set. Database fields
can be accessed by their name using the RecordSet!FieldName schema, by their
index RecordSet.Fields(intIndex) or sometimes by their name from the fields
collection RecordSet.Fields("FieldName").
ITEP004
RECORDSET OBJECT KEY PROPERTIES
LOCKTYPE (LockTypeEnum)
sets the lock type on records when they are open for editing.
RECORDCOUNT (Long)
Returns the number of records in an open RecordSet. If for some reason ADO
cannot determine the number of records then this will be set to -1.
ITEP004
RECORDSET OBJECT KEY METHODS
ADDNEW - Sets up an open record set to add a new record, once the
required values have been set call the Update (or UpdateBatch) method to
commit the changes.
CLOSE - Closes an open RecordSet object, make sure that any changes are
committed using the Update (or UpdateBatch) method before closing or an error
will be generated.
ITEP004
RECORDSET OBJECT KEY METHODS
MOVELAST - Causes an open RecordSet object to move to the last record in
the collection.
OPEN - Opens the RecordSet, typically this is done with a valid Command
object that includes the command text (or SQL) to get the records you want.
ITEP004
WHAT ARE CURSORS?
In ADO, when we talk about cursors, we are essentially talking about a
set of rows.
When you execute a query that returns rows of data, such as SELECT *
FROM mytable, the resulting data is handled using a cursor.
A cursor can be located either on the client with the adUseClient
argument, or on the server with the adUseServer argument.
In addition, the are 4 types of
cursor: adOpenForwardOnly, adOpenStatic, adOpenDynamic,
and adOpenKeyset.
ITEP004
WHAT ARE CURSORS?
ITEP004
CURSOR LOCATION OPTIONS
adUseClient (Client-Side)
A client-side cursor uses local machine resources to implement a cursor and its
set of records.
The advantages of client-side cursors are as follows:
Because they run locally, they provide better performance when their result sets
are a reasonable size.
Client-side cursors generally provide better scalability, because their
performance depends on each client, and not on the server. Therefore, client-side
servers place less of a growing demand on the server as the number of a
system's users increases.
DATABASE MANAGEMENT SYSTEM
ITEP004
CURSOR LOCATION OPTIONS
adUseClient (Client-Side)
The disadvantages of client-side cursors are as follows:
When the rowset returned with the cursor is very large, the local workstation's
resources may be "swamped" by the need to handle the high volume.
Because a client-side cursor must bring all the data for its rowset over the
network, larger result sets can increase network traffic.
ITEP004
CURSOR LOCATION OPTIONS
adUseServer (Server-Side)
A server-side cursor uses server resources to implement a cursor and its set of
records.
The advantages of server-side cursors are as follows:
Local workstation resources are never "swamped" by unexpectedly large
rowsets.
Because a server-side cursor does not transfer all the data in the rowset to the
workstation, there is less network traffic with large rowsets when they are opened
and less delay in opening them.
ITEP004
CURSOR LOCATION OPTIONS
adUseServer (Server-Side)
The disadvantages of server-side cursors are as follows:
For smaller rowsets with a lot of activity performed by the application, serverside cursors do not perform as well, because each request to move the cursor
and each response must travel over the network. It would be better to just transfer
the smaller rowsets to the client to start with.
As users are added to the system, the server receives a greater and greater
resource demand as more and more concurrent users open server-side cursors.
Server-side cursors therefore typically provide less scalability than client-side
cursors.
ITEP004
LOCK TYPES
The lock type property specifies how we are going to lock the underlying data to
protect any changes we make and ensure they are processed.