Tuesday, October 19, 2010

QTP Script For Connecting to Database

QTP Script for connecting to MS Access.

Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
 con.provider="microsoft.jet.oledb.4.0"
 con.open"d:testdata.mdb"
 rs.open"select*from emp",con
Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext Loop
The database we are using here is MS Access.Before running this script create a table in MS Acess.
 In the above script I used table called "emp" and column names as "v1" and "v2". "d:testdata.mdb" is path of the table which we created. The main use of this script is to use testdata of table(which is in database) in the application. In the above script we are passing values from database to Textboxes in Windows Application.

Similarly script for connecting to other 2 databases are

 QTP Script for connecting to sqlserver.

Option Explicit
 Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset") con.open"provider=sqloledb.1;server=localhost;uid=sa;pwd=;database=testdata"
rs.open"select*from emp",con
Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
 rs.movenext
 Loop

Script for connecting to oracle

Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open"provider=oraoledb.1;server=localhost; uid=scott;pwd=tiger;database=testdata" rs.open"select*from emp",con
Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop
This is the way u connect to database in QTP and get the values from database.

No comments:

Post a Comment