QTP Database Scripting
QTP Database Scripting
www.gcreddy.com
Following are some of the key objects found in the ADO object model and some of
their key methods and properties.
Connection Object
This object represents an open connection to the data source. This connection can be
a local connection (say App.Path) or can be across a network in a client server
application. Some of the methods and properties of this object are not available
depending on the type of data source connected to.
Command Object
RecordSet Object
1) Get Test Data from a Database and use in Data Driven Testing (through
Scripting)
1) Dim con,rs
2) Set con=createobject(“Adodb.connection”)
1
Visit: www.gcreddy.com for QTP Information
3) Set rs=createobject(“Adodb.recordset”)
4) con.provider=(“microsoft.jet.oledb.4.0″)
9) Dialog(“Login”).Activate
12) Dialog(“Login”).WinButton(“OK”).Click
14) rs.movenext
15) Wend
1) Dim con,rs
2) Set con=createobject(“adodb.connection”)
3) Set rs=createobject(“adodb.recordset”)
4) con.provider=”microsoft.jet.oledb.4.0″
6) rs.open”select*from Login”,con
7) Set ex=createobject(“Excel.Application”)
9) Set b=a.worksheets(“sheet1″)
10) i=1
2
Visit: www.gcreddy.com for QTP Information
13) b.cells(i,2).value=rs.fields(“password”)
14) rs.movenext
15) i=i+1
16) Loop
17) a.save
18) a.close
Dim objCon,objRs,ObjFso,myFile,myData,rc,r
Set objCon=createobject(“Adodb.connection”)
Set objRs=createobject(“Adodb.Recordset”)
set objFso=createobject(“Scripting.Filesystemobject”)
Set myFile=objFso.OpenTextFile(“C:Documents and SettingsgcrMy
Documentsgcreddy.txt”,8)
objcon.provider=(“Microsoft.jet.oledb.4.0″)
objcon.open”C:Documents and SettingsgcrMy Documentsgcreddy.mdb”
objrs.open “select * from login”,objCon
r=1
Do until objRs.EOF
a=objRs.Fields (“Agent”)
b=objRs.Fields (“Pwd”)
myFile.Writeline a &”,”& b
r=r+1
objRs.MoveNext
Loop
myFile.Close
objCon.Close
Const adOpenStatic = 3
Const adLockOptimistic = 3
objConnection.Open _
3
Visit: www.gcreddy.com for QTP Information
“User ID=fabrikamkenmyer;Password=34DE6t4G!;”
objRecordSet.MoveFirst
Wscript.Echo objRecordSet.RecordCount
Const adOpenStatic = 3
Const adLockOptimistic = 3
objConnection.Open _
“Northwind;fabrikamkenmyer;34ghfn&!j”
objRecordSet.MoveFirst
Wscript.Echo objRecordSet.RecordCount
Const adOpenStatic = 3
Const adLockOptimistic = 3
objConnection.Open _
4
Visit: www.gcreddy.com for QTP Information
“Data Source=inventory.mdb”
objRecordSet.MoveFirst
objRecordSet2.MoveFirst
Do Until objRecordset.EOF
Wscript.Echo objRecordset.Fields.Item(“ComputerName”)
Wscript.Echo objRecordset.Fields.Item(“OSName”)
objRecordSet.MoveNext
Loop
Do Until objRecordset2.EOF
Wscript.Echo objRecordset2.Fields.Item(“DriveName”), _
objRecordset2.Fields.Item(“DriveDescription”)
objRecordSet2.MoveNext
Loop
objRecordSet.Close
objRecordSet2.Close
objConnection.Close
Const adOpenStatic = 3
Const adLockOptimistic = 3
5
Visit: www.gcreddy.com for QTP Information
objConnection.Open _
adLockOptimistic
objRecordSet.MoveFirst
objRecordSet.Close
objConnection.Close
Dim objCon,objCom
Set objCon=Createobject(“ADODB.connection”)
objCon.open”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:gcreddy.mdb;”
Set objCom=Createobject(“ADODB.Command”)
objCom.ActiveConnection=objCon
objCom.Execute
objCon.Close
Set objCom=Nothing
Set objCon=Nothing
9) Insert multiple sets of Data (using Excel sheet) into a database table
using Database Command Object
6
Visit: www.gcreddy.com for QTP Information
Dim objCon,objCom,strEmpName,intEmpNo,intEmpSal,intRowcount,i
Set objCon=Createobject(“ADODB.connection”)
objCon.open”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:gcreddy.mdb;”
Set objCom=Createobject(“ADODB.Command”)
objCom.ActiveConnection=objCon
Datatable.AddSheet(“input”)
Datatable.ImportSheet “C:gcreddy.xls”,1,”input”
intRowcount=Datatable.GetSheet(“input”).GetRowCount
Msgbox intRowcount
DataTable.SetCurrentRow(i)
strEmpName= DataTable.Value(1,”input”)
intEmpNo= DataTable.Value(2,”input”)
intEmpSal= DataTable.Value(3,”input”)
objCom.Execute
Next
objCon.Close
Set objCom=Nothing
Set objCon=Nothing
www.gcreddy.com