 |
Professional ADO RDS
Programming with ASP
by Matt Brown, Charles Caison, John Papa, Peter DeBetta, Eric Wilson
Published by Wrox Press 
1999; $49.99US; ISBN: 1861001649
500 pages
Sample Chapter 6: Building on the Recordset Foundation
Copyright 1999. Wrox Press, reprinted with permission. The text format was modified to
fit the format of this web site.
|
Related Links
In-depth review of the book
The Supports Method
Your recordset can tell you what properties are supported by its
OLE DB provider. Keep in mind though, that while the provider may support some
functionality, a particular recordset might not. For example, you might have a recordset
that presents a calculated field that does not correspond to an actual field in the
database. While the provider may support updating for a recordset of your configuration,
it will not be able to update that particular recordset.
When you call the Supports method, you have a number of options:
booResult = objRS.Supports(Options)
The following table lists the options that you can query using
the Supports method:
| Attribute
Name |
Description |
Option
Constant |
| Add new |
When Supports returns
true, the provider allows for the AddNew method to be called for adding a new empty record
to the recordset. |
adAddNew |
| Approximate position |
When true, the
provider allows for the use of the AbsolutePage and AbsolutePosition properties. |
adApproxPosition |
| Bookmarkable records |
When true, the
provider allows for the use of bookmarks in the recordset (a bokmark is a variant value
returned from the Bookmark property, that uniquely identifies a record bookmarks
can be used to return to an exact location in a recordset). |
adBookMark |
| Deletable records |
When true, you may
use the Delete method of a recordset to remove rows from the recordset. |
adDelete |
| Find |
When true, you can
use the Find method to search a column in the recordset for a specific value. |
adFind |
| Hold |
When true, you can
request more rows or move the row pointer without committing your edits. |
adHoldRecords |
| Move back |
When true, you can
use MovePrevious, MoveFirst, GetRows and Move to move the record pointer backwards. |
adMovePrevious |
| Resynchronize recordset |
When true, the provider allows
you to call the Resync method. The Resync method refreshes the recordset with any changes
in the underlying data in the database, no new records are included in the Resync. |
adResync |
| Updateable |
When true, the provider will
allow for changes to be made with the Update method of the recordset. |
adUpdate |
| Batch updateable |
When true, the underlying
provider supports UpdateBatch and CancelBatch methods for the cursor type. |
adUpdateBatch |
An Example
The code sample below is a neat way of creating recordsets with
different permutations of cursor types, lock types, and cursor locations, and finding out
what each recordset supports based on those choices.
It's quite straightforward really. We'll use a simple HTML form
to choose the lock type, cursor type and cursor position of the recordset, like this:
<FORM NAME="Supports" ACTION="Supports.asp"
METHOD="POST">
Cursor Type <BR>
<SELECT name="ct">
<OPTION value=0 SELECTED>Forward Only
<OPTION value=1>Keyset
<OPTION value=2>Dynamic
<OPTION value=3>Static
</SELECT><P>
Cursor Location<BR>
<SELECT name="cl">
<OPTION value=2 SELECTED>Server Side
<OPTION value=3>Client Side
</SELECT><P>
Lock Type<BR>
<SELECT name="lt">
<OPTION value=1 SELECTED>Read Only
<OPTION value=2>Pessimistic
<OPTION value=3>Optimistic
<OPTION value=4>Batch Optimistic
</SELECT><P>
<INPUT ALIGN=CENTER TYPE=SUBMIT VALUE="View Support">
</FORM>
| So our page looks like this to start: |
 |
Hitting the View Support button will submit our form. After some
unexciting variable declarations (which we'll skip here) we set the values for
strLocation, strLock and strType the variables that we'll use in a moment to open a
recordset:
<%
if Request("ct") <> "" then
<variable declarations>
SELECT CASE intCursorLocation
CASE 2 strLocation = "Server Side"
CASE 3 strLocation = "Client Side"
END SELECT
SELECT CASE intLockType
CASE 1 strLock = "Read Only"
CASE 2 strLock = "Pessimistic"
CASE 3 strLock = "Optimistic"
CASE 4 strLock = "Batch Optimistic"
END SELECT
SELECT CASE intCursorType
CASE 0 strType = "Forward Only"
CASE 1 strType = "Keyset"
CASE 2 strType = "Dynamic"
CASE 3 strType = "Static"
END SELECT
We'll set the SQL string, and open the connection to the pubs
database, as we've seen before in this chapter. Then we can query the recordset against
the database using the strLocation, strLock and strType variables for the
recordset's settings:
strSQL = "SELECT * FROM authors" 'Initialize the string
objConn.CursorLocation = intCursorLocation 'Connect to database
objConn.Open "Provider=MSDASQL;" & _
"Persist Security Info=False;" & _
"User ID=sa;" & _
"Data Source=winbook;" & _
"Initial Catalog=pubs"
Set objRS = CreateObject("ADODB.Recordset") 'Create the recordset object
objRS.CursorType = intCursorType
objRS.LockType = intLockType
Set objRS.ActiveConnection = objConn
objRS.Open strSQL
In this example, we're not interested in the results of the query
we're interested in what funtionality the recordset supports. So the rest of the
page is given over to presenting the attributes, and whether-or-not they are supported by
objRS:
'Test Supports
Response.Write "<HR><H2>Previous test: </H2>" & _
"Cursor Type = " & strType & "<BR>"
& _
"Lock Type = " & strLock & "<BR>"
& _
"Location = " & strLocation & "<BR>"
& _
"<H2>Supports:</H2>"
Response.Write "<TABLE BORDER=1>"
Response.Write "<TR><TD>AddNew</TD>"
Response.Write "<TD>" & objRS.Supports(&H1000400) &
"</TD></TR>"
Response.Write "<TR><TD>Approx Position</TD>"
Response.Write "<TD>" & objRS.Supports(&H4000) &
"</TD></TR>"
<etc>
Response.Write "<TR><TD>Update Batch</TD>"
Response.Write "<TD>" & objRS.Supports(&H10000) &
"</TD></TR>"
Response.Write "</TABLE>"
end if
%>
</BODY></HTML>
Here are the results. The output contained in
this table is dependent on the choices that the user makes for the recordset's cursor
type, cursor location and lock type.
The figure shows the no-frills approach of using a forward-only,
read-only cursor. This script will allow you to test any combinations that you wish
remember it is using the SQL Server OLEDB provider. |
 |
|