8 |
The LA Fox Developer Newsletter
February 2000
ADO Quickstart
(Con't from page 7)
when using Remote Data Services (RDS)
Any additional arguments passed in the ConnectionString are
passed through to the OLE DB provider being used.
In addition to the Open method, the following are the common
methods you are likely to use with the Connection object:
+
BeginTrans, CommiTrans, and RollBackTrans—These
methods work like the Begin Transaction, End Transaction, and
RollBack statements in Visual FoxPro. The Connection object
controls all transaction processing. For more detail, see the
section Transactions/Updating Data. Note that not all OLE
DB providers support transaction processing.
+
Close—This method closes an open Connection object.
+
Execute—This method runs a SQL statement, stored proce-
dure, or OLE DB provider-specific command. In reality, a
Command object, which actually does the work of executing the
command, is created on the fly. More on the Command object
and the flat object hierarchy of ADO later in this paper.
+
OpenSchema—This method returns information regarding
defined tables, fields, catalogs, and views into an ADO
Recordset object. This method works like the DBGetProp()
function in Visual FoxPro.
Errors collection
ADO does not trap errors, nor does it have an error handler.
Instead, ADO can record the occasions when errors occur. It is
up to the host application, Visual FoxPro in this case, to both
trap and handle the error. ADO only reports what errors have
occurred. Note that the error is actually reported by the specific
OLE DB provider. ADO is merely a vehicle to report the error.
The Errors collection is part of the Connection object and
consists of zero or more Error objects. When an error occurs,
an Error object is appended to the Errors collection. The
following code illustrates how the Errors collection works. In this
example, the name of the database has been misspelled
purposely in order to generate an error:
oConnection
=
CreateObJect(”adodb.connection”)
With oConnection
Provider
=
“SQLOLEDB.1”
.connectionStrlng
=
“Persist Security Info=False;User
ID=sa;Initlal Catalog=Nothwind;Data Source=JVP”
.Open
EndWith
*~
At this point an error will occur
—
causing VFP’s default error
*/
handler
—
or the active error handler to invoke
*/
At this point, we can query the Errors Collection of the
*/
Connection Object
For Each Error In oConnection.Errors
?Error.Description,Error.Number
Next Error
Recordset Object
ProgID: ADODB.Recordset
Once you establish an ADO connection, you can open a
recordset of data. The Recordset object is very much like a
Visual FoxPro cursor. Like the Visual FoxPro cursor, an ADO
recordset consists of rows of data. The recordset is the primary
object that you will use while working with ADO. Like the
Connection object, the Recordset object also provides an Open
method. To illustrate, the following code opens the Customer
table of the Visual FoxPro Tastrade database:
oRecordSet
=
C reateObject( “adodb.recordset”)
oRecordSet.Open(”Select
*
From Customer” oConnection)
The first argument of the Open method specifies the source of
data. As you will see, the source can take on several forms.
The second argument of the Open method specifies a connec-
tion to use for retrieving the data specified by the source. At a
minimum, this is all you need to open a recordset. Additional
examples will expand on the additional arguments the Open
method accepts.
With a Recordset object created, one of the most common
actions you will perform is navigating through records. Depend-
ing on the type of ADO recordset that has been created, certain
navigational capabilities may or may not be available. The
different types of possible ADO recordsets will be discussed
shortly. The following code illustrates how to navigate through
an ADO recordset:
Do While IoRecordSet.Eof
oRecordset.MoveNext
EndDo
The following paragraphs briefly describe the most common
recordset properties and methods you are likely to use. It is by
no means a replacement for the ADO documentation, which
gives both a complete description of the properties and methods
and complete descriptions of acceptable enumerated types and
arguments. ADO is well documented in the Microsoft Data
Access Components (MDAC) SDK. You can download the
MDAC SDK from http://www.microsoft.com/data/mdac2.htm
(Con't, page 9)
Page 8
|
8 |