Wednesday, August 19, 2009

QTP & SQL-Server Database Connection

The below QTP script shows a simple example of connecting QTP with SQL Server. For the purpose of this example I have created a simple table with three columns as shown below. The QTP script here is showing the values of second column in the message box one by one. In the below script do not forget to use your own server, username, password, database etc.








Option Explicit
Dim con,rs
'The ADO Connection Object is used to create an open connection to a data source.
Set con=createobject("adodb.connection")
'The ADO Recordset object is used to hold a set of records from a database table.
'A Recordset object consist of records and columns (fields). When you first open a Recordset,
'the current record pointer will point to the first record and the BOF and EOF properties are False.
'If there are no records, the BOF and EOF property are True.
Set rs=createobject("adodb.recordset")
'Below it shows provider, server, username, password, database
con.open"provider=sqloledb.1;server=server2008;uid=rts_dev1;pwd=password;database=model"
'See all the parameters to recordset "Open" command - http://www.w3schools.com/ado/met_rs_open.asp
rs.open"select * from person",con
Do while not rs.eof
'will show the SECOND column values
msgbox rs.fields.Item(1)
rs.MoveNext
Loop
con.Close


Also See:

Connecting QTP to Oracle and running a Query