how to read/retrieve data from database using QTP?
Tags:
Question ID: 105810
0
0
Marked as spam
Posted by (Questions: 17, Answers: 7)
Asked on January 16, 2015 8:50 am
574 views
Answers (1)
1
Private answer

You can use the ADODB object and the Recordset object to run database queries and retrieve data. The steps are pretty much the same for each database. The connection string is sometimes the tricky part and depends on the database you want to work with. The best place to find examples of database connection strings is www.connectionstrings.com

Here are a few examples:

'Exporting some data from a SQL Server db to a text file

Set objCon=createobject(''Adodb.connection'')
Set objRs=createobject(''Adodb.Recordset'')
set objFso=createobject(''Scripting.Filesystemobject'')

Set myFile=objFso.OpenTextFile(''C:database.txt'',8)
connstring = ''Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;'' 'just a dummy string
objcon.open connstring
objrs.open ''select * from users'',objCon
r=1
Do until objRs.EOF
f=objRs.Fields (''First'')
l=objRs.Fields (''Last'')
myFile.Writeline f &'',''& l
r=r+1
objRs.MoveNext
Loop
myFile.Close
objCon.Close

'Display the results of a database query against SQL Server Express edition

'Create the database connection object
Set objConnection = CreateObject(''ADODB.Connection'')

'Create RecordSet Object
Set objRecordSet = CreateObject(''ADODB.Recordset'')

'Query to be Executed
DBQuery = ''Select NAME from dbo.USERS where ID = 199''

'Connect using SQL OLEDB connection string
objConnection.Open ''Provider=sqloledb.1;Server=.SQLEXPRESS;User Id=sa;Password=PasswordXYZ;Database=Samples''

'Execute the query
objRecordSet.Open DBQuery,objConnection

'Retrieve the result set
Value = objRecordSet.fields.item(0)
msgbox Value

'Release the objects from memory
objRecordSet.Close
objConnection.Close

Set objConnection = Nothing
Set objRecordSet = Nothing

Marked as spam
Posted by (Questions: 2, Answers: 477)
Answered on April 1, 2015 2:48 pm
EyeOnTesting

Welcome back to "EyeOnTesting" brought to you by Orasi Software, Inc.

X
Scroll to Top