Docsity

ADO Recordsets: Creating and Manipulating Data with ADO, Slides of Fundamentals of E-Commerce

Partial preview of the text

Download ADO Recordsets: Creating and Manipulating Data with ADO and more Slides Fundamentals of E-Commerce in PDF only on Docsity!

The RecordSet Object

• Used to return rows to the client.• #1 most used part of ADO• Can be created alone or with a connection

object to "pool" connections.

• Can be used to execute stored procedures

without parameters.

• Can be created w/o a connection object• Is created from "ADODB.Recordset"

Using the connection object to

return a recordset

' You can use a select statement to 'create' a snapshot forward only,' read only cursor. (A "firehose" cursor)dim rs

'as ADODB.Recordset

dim mySQL

'as string

mySQL = "Select * from authors"Set rs = CN.Execute mySQL'... Use cursorrs.Closeset rs = nothing

Example: R/W (Updateable)

Cursor

Dim mySQL as StringDim rs As New ADODB.Recordset'Assume CN is an ADODB.Connection Object and is OpenmySQL = "Select * From Authors"'Notice use of SET verbSet rs.ActiveConnection = CNrs.CursorLocation = adUseClientrs.CursorType = adOpenDynamicrs.LockType = adLockOptimisticrs.Source = mySQLrs.Open

Recordset Methods

•^

.MoveFirst, .MoveLast, .MovePrevious, .MoveNext(moves cursor in rows, error if

.EOF

or

.BOF

or if

.RecordCount

•^

.Move

n (

moves number of rows specified by, see note

above)

-^

.AddNew (adds a new row to recordset, applies defaults ifany, needs to be followed by

.Update

•^

.Update (commits changes to .Recordset)

-^

.UpdateBatch (use in addition to update with batchlocking)

Explanations #1a

• EOF and BOF are functions that return true

if we are at the end or beginning of arecordset. To check to make sure we areactually on a good (usable record) weshould:

If not (rs.EOF or rs.BOF) Then

Explanations #1b

• The .RecordCount property returns the

number of records:^ -1: Unknown (Any updatable recordset has this value (use

the previous EOF/BOF test) 0: No records1…n (only valid for static type (read only) recordsets)

• Never use RecordCount to loop• Always test for <> 0, not a value

Recordsets w/o connection

objects.

Set rs = Server.CreateObject("ADODB.Recordset")' Not no SET, creates a unique connectionrs.ActiveConnection = cDSN

'DSNless string

rs

.CursorLocation = adUseClient rs

.CursorType = adOpenStatic rs

.LockType = adLockBatchOptimistic rs

.Source = mySQL rs

.Open 'If you only need one recordset, period, use this.