190 likes | 357 Views
Recordsets. Colorado Technical University IT420 Tim Peterson. 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. Cursors.
E N D
Recordsets Colorado Technical University IT420 Tim Peterson
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
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
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
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
Implicit Recordset Creation <% Option Explicit Dim strConnect %> <!-- #INCLUDE FILE="DataStore.asp" --> <HTML> <HEAD> <TITLE>ADO Recordset Object</TITLE> </HEAD> <BODY> <% 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 %> </BODY> </HTML>
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: <!-- METADATA TYPE=“typelib” File=“C:\Program Files\Common Files\System\ado\msado15.dll” -->
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.
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.
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.
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.
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
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.
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
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.
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’”
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.
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.
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.