How to fetch data from database in qtp

To retrieve the data from a database the below code will help you.

To understand the below code, it is been assumed that, the tester already knows about his database schema, tables, SQLs, Data Source Names

'Create the objects for retrieving the data
Dim objDB, objResults

'Create an object for the database connection
Set objDB = CreateObject("ADODB.Connection")

'Enter the Connection string, where the Data Source Name (DSN) is pointed. Create if the DSN is not created
objDB.ConnectionString = "DSN=QT_Flight32"

'Open the DB Connection
objDB.open

'Prepare a SQL Statement
strSQL = "Select * from Flights where departure= 'San Francisco'"

'Execute the SQL statement and capture the results in a Results area
Set objResults = objDB.Execute(strSQL)

'Iterate until the end of results reaches
Do Until objResults.EOF

'Retrieve the Columns (Arrival, Departure) values of your table (Flights)
strArrival = objResults.Fields("Arrival")
strDeparutre = objResults.Fields("Departure")

'Write the results to Report
Reporter.ReportEvent micDone, strArrival & ":" & strDeparture, ""

'Move the cursor to the next row
objResults.MoveNext

Loop

'Close the cursor
objResults.Close

'Close the connection
objDB.Close

'Destroy the objResults object
Set objResults = Nothing

'Destroy the objDB object
Set objDB = Nothing