In Part1 we saw the wizard method to use Database checkpoint.  In Part2 we learned how to connect to a database using scripts in QTP. In this part we will see how we can retrieve data from database using scripts.

Similar to connection object, you can use ADODB.Recordset to get the recordset object.

Set MyRecordSet = CreateObject("ADODB.Recordset")

Before starting let us first understand the properties and methods of recordset object which will help further.

RecordSet Properties Description
AbsolutePage Page of current position
AbsolutePosition Current position
ActiveConnection Active Connection Object
BOF Beginning of File
Bookmark Bookmark of current position
CacheSize Number of records cached
CursorLocation Server or Client
CursorType Forward, Static, Dynamic, Keyset
EOF End of File
EditMode Whether or not an edit is in progress
Filter What kind of records to hide
LockType Record lock type for edits or updates
MaxRecords Maximum number of records to retrieve
PageCount Total number of pages
PageSize Number of records per page
RecordCount Total number of records
Source Source Command Object
Status Status of last action

Recordset Methods Description
AddNew Add a new record to the RecordSet
CancelBatch Cancel pending batch updates
CancelUpdate Cancel pending update
Clone Copy the RecordSet
Close Close the RecordSet
Delete Delete current record
GetRows Retrieve multiple records
Move Move the record pointer to a specific record
MoveNext Move the record pointer to the next record
MovePrevious Move the record pointer to the previous record
MoveFirst Move the record pointer to the first record
MoveLast move the record pointer to the last record
NextRecordSet Load the next RecordSet in a multi-set query
Open Open the RecordSet (execute the query)
Requery Re-execute the last query executed
Resync Synchronize the data with the server
Supports Determine if a feature is supported by provider
Update Update the current record
UpdateBatch Update pending batched record updates

Don’t worry if you feel this listing is too big, you may never use many of these, all this is for your reference in case you need. The most commonly used property and functions are:

  • EOF – to identify no records returned. You run a query and need to determine the pointer is at last record or not or no records has been returned.
  • Open – To retrieve the RecordSet we use open method which requires two arguments – connection object and command object

Syntax for this would be

<RecordSetName>.Open source, connection, cursor, lock, type

 

  • Source – is actually the command object, it could be a sql statement
  • Connection – is the connection object connecting the database
  • Cursor – optional parameter to define recordset cursor type (default – Forward only)
  • Lock – optional parameter to set the lock type property(default – Read Only)
  • Type – optional parameter to define the command type (default unknown(8))

 

For example

Set oRecordSet = CreateObject(&quot;ADODB.Recordset&quot;)
oRecordSet.Open “Select * from Orders”,oConnection, adOpenStatic

here oConnection is connection session to the database , we have used the static cursor which allows us to open the record in readonly mode, and not used a lock type – so it will use the default lock.

a generic function for retrieving the recordset would be like

Function 2

Function getRecordset(strSQL)
Dim oConnection, oRecordSet
Set oConnection = CreateObject(&quot;ADODB.Connection&quot;)
Set oRecordSet = CreateObject(&quot;ADODB.Recordset&quot;)
oConnection = getConnection()
oRecordSet.Open strSQL,oConnection, adOpenStatic Set getRecordset = oRecordSet
End Function

Let us now try this to retrieve data from Orders table of Flight application database.

To get the customer name from table Orders with Order Number 1 will need to execute the query –
S

Select * from Orders where Order_Number = 1

Example 2

Set MyRecordset = getRecordset(&quot;Select * from Orders where Order_Number = 1&quot;)

If MyRecordset.EOF &lt;&gt; True Then
msgbox MyRecordset.Fields(&quot;Customer_Name&quot;).Value
End If

Result

getRecordset method in Database Checkpoint

In this example we are using the function 2 by passing the required query and we get the result recordset in the MyRecordset object. Then we are checking whether there is some records there or not using the EOF property.

In the same way we can use other methods as well for different operations

Move to next record – We have a method MoveNext which we can use in a loop until the max record in recordset. Below example illustrates the use of this method to get all the records from table

Example 3

Set MyRecordset = getRecordset(&quot;Select * from Orders&quot;)

Do while MyRecordset.EOF &lt;&gt; True
print MyRecordset.Fields(&quot;Customer_Name&quot;).Value
MyRecordset.MoveNext
Loop

Result

Move to next record - MoveNext Method

In the same way you can use other methods and properties of RecordSet object to achieve your task. I have listed her some example which you may encounter in your test.

Function 3 – Number of Records We can traverse through all the records in the recordset to get the

Function getRecordCount(ByRef RecordSet)
Dim Rows
Rows = 0
RecordSet.MoveFirst
Do Until RecordSet.EOF
Rows = Rows+1
RecordSet.MoveNext
Loop
getRecordCount = Rows
End Function

Function 4 – Execute Query

If you need to update, insert or delete records from database, you can use this function. But if you are working on the database for application under test, you should not perform such actions until unless it is required or a database used for QTP.

Function ExecuteQuery(strSQL)
On Error Resume Next
Set oConnection = CreateObject(&quot;ADODB.Connection&quot;)
oConnection = getConnection()
oConnection.Execute strSQL
If Err.Number &lt;&gt; 0 then
ExecuteQuery = False
Exit Function
End If
ExecuteQuery = True
End Function

Example 4 – Retrieve the column names from a table

Set MyRecordset = getRecordset(&quot;Select * from Orders&quot;)

nColumns = MyRecordset.Fields.Count
For n = 0 to nColumns - 1
Print MyRecordset.Fields(n).Name
Next

Retrieve the column names of a table
Example 5 – Import Database Table into Data table

‘ Get recordset
Set MyRecordset = getRecordset(&quot;Select * from Orders&quot;)
‘Get number of columns in table
nColumns = MyRecordset.Fields.Count
‘Add a sheet in your datatable
Datatable.AddSheet (&quot;DBImport&quot;)
For n = 0 to nColumns – 1
‘Get column name
ParamName = MyRecordset.Fields(n).Name
‘Add DTParameter
Datatable.GetSheet(&quot;DBImport&quot;).AddParameter ParamName,&quot;&quot;
nRow = 1
MyRecordset.MoveFirst
‘Retrieve and place data in data table
Do while MyRecordset.EOF &lt;&gt; True
Datatable.SetCurrentRow(nRow)
Datatable(ParamName,&quot;DBImport&quot;) = MyRecordset.Fields(ParamName)
nRow = nRow + 1
MyRecordset.MoveNext
Loop
Next

Database table into Data table

Apart from this, if you need to connect to a excel file using ADODB connection, you will have to specify a different connection string, which will be like

&quot; DRIVER={Microsoft Excel Driver (*.xls)};DBQ= FileName ;Readonly=True&quot;

With this Saket concludes the series on Database Checkpoint and QTP.