Of the ADO objects, the Recordset object does most of the data processing and thus it has more properties, methods, and events than the other ADO objects.
If you are not returning a recordset, then you should probably use the Connection or Command object. If you are returning row(s) in a recordset, then the Recordset object is for you.
In Visual Basic this is done with syntax similar to the following:
Dim rstObject1 as New ADODB.Recordset Dim rstObject2 as ADODB.Recordset
In VBScript this is done with syntax similar to the following:
Set rstObject1 = Server.CreateObject("ADODB.Recordset")
In JavaScript/JScript this is done with syntax similar to the following:
rstObject1 = new ActiveXObject("ADODB.Recordset");
The basic syntax for opening a Recordset is as follows:
recordset.Open Source, ActiveConnection, CursorType, LockType, Options
Source. A Command object or an expression for a SQL statement, table, or stored procedure.ActiveConnection. A Connection object or an expression for a connection string that might be used to make Connection object.CursorType. ADO only allows four cursors. ADO also lets you decide the cursor location (via the CursorLocation property), but if a client-side cursor is chosen, then it has to be a Static cursor.
LockType. ADO supports 4 kinds of record locking.
Options. Multiple options can be selected which define what kind of command the Source parameter contains and how to execute the Recordset object.
Here are some quick notes regarding moving through the rows in recordsets.
Here are some Recordset properties that can move through a recordset:
The Move() method has this syntax:
recordset.Move NumberOfRecords, start
NumberOfRecords. If the start parameter is not set, then this is a positive or negative number that indicates rows relative to the current row.start. The row from which the move should occur.
The Seek() method has this syntax:
recordset.Seek KeyValues, options
KeyValues. An array of variant values corresponding to the one or more columns that form the index of a recordset. Set the index column name(s) by setting the Index property.options. The type of comparison between the KeyValues and the index.
KeyValues .KeyValues KeyValues or just after it would have occurred.KeyValues would have occurred.KeyValues or just before it would have occurred.KeyValues would have occurred.A typical VB parsing of a Recordset:
Do Until rst.EOF
Response.Write "<option>" & rst.Fields("OrganizationName").Value & "</option>"
If rst.Fields("OrganizationName").Value <> "Berwyn" Then Exit Do
grst.MoveNext
Loop
Now that you're on the right row, access the column(s) in the row. The syntax is as follows:
recordset.Fields(index).property
index. Either the field name or its ordinal position.property. Any of the field properties.This is essentially an ADO Field object.
These syntaxes are equivalent:
recordset.Fields("FieldName").Value
recordset.Fields(IndexNumber).Value
recordset(IndexNumber)
recordset!FieldName
At time it is advantageous to loop through the Fields collection. EG:
For i = 0 to rst1.Fields.Count - 1 str1(i) = rst1.Fields(i).Name 'Column name str2(i) = rst1.Fields(i) 'Cell value Next
Now that you're on the right row, change some data in the fields and update the database with the new data.
This is usually performed by passing values to columns in rows and then either moving from the current row or using an update method (This assumes that the Recordset was not opened with adLockReadOnly locking.). EG:
rst1("FirstName") = "George"
rst1.Update()
rst1("LastName") = "George"
rst1.MoveNext()
If the Recordset was opened with adLockBatchOptimistic locking, then you may update batches of rows by using the UpdateBatch() method which has this syntax:
recordset.UpdateBatch AffectRecords
AffectRecords. The rows affected by the batch update.
If you have passed values to columns in rows but have not updated the data source yet you can cancel the changes so far. After a cancellation, you should make sure the recordset is on a known row.
recordset.CancelUpdate
record.fields.CancelUpdate
recordset.CancelBatch AffectRecords
If you have enclosed the whole affair in an ADO transaction, then you can rollback updates.
There can be a number of problems that turn up when updating rows:
Now that you're on the right row, delete the row, and then move to a known row.
recordset.Delete AffectRecords
Note that if all you are doing is deleting a row in a table, eg deleting old rows in a log, then you may be better off just using the Connection.Execute() method.
The process of adding a row to a recordset involves using the Recordset.AddNew() method which moves you to a new row (if you are currently editing a row when the method is called then that row may be updated!). From the new row you have to pass the appropriate values to the fields and then update the recordset.
recordset.AddNew FieldList, values
FieldList. Optional. A single field name or array of field names or array of ordinal positions.values. Optional. A corresponding value or array of values.Note that if all you are doing is adding a row to a table, eg adding to a log, then you may be better off just using the Connection.Execute() method.
As of ADO 2.1, the Recordset data can be persisted as an XML file.
recordset.Save(DestinationFilename, 1)
<%
Set fso = CreateObject("Scripting.FileSystemObject")
filename = Server.MapPath("my.xml")
If fso.FileExists(filename) Then
fso.DeleteFile(filename)
End If
rst.Save(filename, 1)
%>
As of ADO 2.5, the Recordset data can be persisted as a stream and, hence, a MS XML DOM document.
<% recordset.Save(Response, 1) %>
recordset.Save(DestinationXMLDOMdoc, 1)
Dim xmldoc As New MSXML.DOMDocument rst.Save(xmldoc, 1)
When you are finished with the recordset close it.
recordset.Close()
If you want to reuse the recordset, open it again, but otherwise release the object variable from memory.
Set recordset = Nothing
This table presents the Recordset methods by category in the first column and alphabetically in the second column.
| Properties | |
|---|---|
|
|
| Methods by Category | Methods Alphabetically |
|
|
| Events | |
|
|
Page Modified: (Hand noted: 2008-04-29 21:22:21Z) (Auto noted: 2010-12-24 22:50:38Z)