Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson.

Similar presentations


Presentation on theme: "ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson."— Presentation transcript:

1 ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson

2 ASP-13-2 What is a Recordset? Recordset object is more than just the data. It has functionality which allows us to: –Manipulate data –Add data –Delete data –Hide records –Search for data within a record

3 ASP-13-3 Cursors Cursors are used to find your way around Recordsets. Cursors can point at an individual record object. Names of the field can then be used to identify individual cells of interest. Example cursor methods are: –MoveFirst, MoveLast, and MoveNext

4 ASP-13-4 Recordset creation Recordsets can be create via various methods. Dim objRS Set objRS = Server.CreateObject(“ADODB.Recordset”) To create the connection: Dim strConn, objConn, objRS strConn = “Provider=Microsoft.Jet-OLEDB.4.0;Data Source=C:\Movie2000.mdb Set objConn = Server.CreateObject(“ADODB.Connection”) objConn.Open strConn Set objRS = Server.CreateObject(“ADODB.Recordset”) objRS.Open “Movies”, objConn

5 ASP-13-5 Avoiding Explicit Connection Objects ADO can hide the interdependence of code, making the coding of connections easier. To open a Recordset directly: Dim objRS, strConnect strConn = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Movie2000.mdb” Set objRS = Server.CreateObject(“ADODB.Recordset”) objRS.Open “Movies”, strConnect

6 ASP-13-6 Implicit Recordset Creation <% Option Explicit Dim strConnect %> ADO Recordset Object <% Const adOpenForwardOnly = 0 Const adLockReadOnly = 1 Const adCmdTable = 2 Dim objRS ' recordset object Set objRS = Server.CreateObject ("ADODB.Recordset") ' create recordset object objRS.Open "Movies", strConnect, _ adOpenForwardOnly, adLockReadOnly, adCmdTable ' now open it While Not objRS.EOF ' now loop through the records Response.Write objRS.Fields("Title") & ", " objRS.MoveNext Wend objRS.Close ' now close it Set objRS = Nothing '...and clean up %>

7 ASP-13-7 ADO Constants ADO has many pre-defined constants which are used to specify options. As opposed to just using the numbers, we define the constants so that the code is easier to read. The constant names are defined in msado15.dll. So that we do not have to define these constants, we can use the dll as follows:

8 ASP-13-8 Open Method Characteristics recordset.Open Source, ActiveConnection, CursorType, LockType, Options Source - Defines where the data comes from. Active Connection - Identifies the data store connection (e.g. strConnect). Cursor Type - By defining the cursor type, we define the functionality we want with the recordset.

9 ASP-13-9 Cursor Types Cursor must be defined when the recordset is opened. Forward-Only - This is the default and only allows us to move forward through a recordset. Static - Similar to forward-only with the additional ability to scroll backwards in the recordset. Dynamic - Allows you to see changes to the recordset that are made by other users (additions, deletions, etc.) Keyset - Similar to dynamic except that you can’t see records added by other people.

10 ASP-13-10 LockType LockType determines if records are updateable. Types of locks are: –Read-only - The recordset can not be updated. This is the default. –Pessimistic - Provides an updateable recordset. –Optimistic - Provides an updateable recordset with a carefree lock. –Optimistic Batch - Allows you to make multiple updates to the recordset and update the data store all at once. Locking is the same as optimistic.

11 ASP-13-11 Options Used to define what what type of data source is being connected to. They are: –adCmdText - Indicates that the source parameter holds a text command such as a SQL statement. –adCmdTable - Indicates the source holds a table name. –adCMDStoredProc - Indicates that source holds the name of a stored procedure or query. –adCmdFile - Indicates source holds the name of a file which is a saved recordset. –adCMDURLBind - Indicates the source holds a URL.

12 ASP-13-12 Cursor Movement MovePrevious and MoveNext - Allows the cursor to move to the immediately preceding or next record. MoveFirst and MoveLast - Moves the cursor to the first or last record in a recordset. Usage is as follows: objRS.MoveLast

13 ASP-13-13 Cursor Movement - Move A fifth method exists Move - Used to jump over a specified number of records. objRecordset.Move NumRecords, Start NumRecords specifies the number of records to jump. Start specifies where in the recordset we want to start from.

14 ASP-13-14 Bookmarks Bookmarks are used for marking a record in a recordset Not all Recordsets support bookmarks. Example bookmark usage code: Dim varMyBookmark ….code to create the Recordset Object varMyBookmark = objRS.Bookmark …Record processing code objRS.Bookmark = varMyBookmark

15 ASP-13-15 Searching Records The Find method is a full table scan. Find syntax is as follows: objRecordset.Find Criteria, SkipRecords, SearchDirection, Start Criteria - defines the string to be found SkipRecords - offset from the start position (0) SearchDirection - either adSearchForward (default) or adSearchBackward Start - a bookmark that specifies a start position.

16 ASP-13-16 Filtering Records Involves finding all occurrences of a record that match a search string. This allows you to hide records of no importance. By default, filtering is turned off. Example filter code: objRS.Filter = “Instructor = ‘Don Adams’”

17 ASP-13-17 Filtering Methods adFilterNone - Turns off all filtering. adFilterPendingRecords - Shows only records that have changed in the recordset but not the data store. (Only works with batch update). adFilterAffectedRecords - Shows only records affected by the last Delete, Resynch, UpdateBatch, or CancelBatch call. adFilterFetchedRecords - Shows records from the last call to retrieve records from the data store.

18 ASP-13-18 Fields Collection This collection allows us to use all of the fields in a recordset. Each Recordset object has it’s own fields collection. We can also find out field names with the Fields collection.

19 ASP-13-19 Recordsets in Arrays GetRows method places recordset data in an array. Code example is as follows: varArray = objRecordset.GetRows (Rows, Start, Fields) Rows - Allows us to restrict number of records placed in the array. Start - This is a bookmark that defines what record to start on. Fields - Specifies the fields to be placed in the array.


Download ppt "ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson."

Similar presentations


Ads by Google