Boost PC performance: How more available memory can improve productivity
Db connection to qtp
1. 7/22/2014 QTPWorld
Chapters
QTPWorld News
QTP Interview Questions
VB Script - Part I
VB Script - Part II
VB Script - Part III
Working with Files using FSO
Excel
Actions
Functions
Difference between Action
and Function
Parameterization
Object Repository
Descriptive Programming
Regular Expression
Error Handling & Recovery
Scenario
Output Values
Checkpoints
Database Connections
Automation Object Model
Synchronization
Environment Variables
XML
Outlook
Reporting Defect
Document Object
Model(DOM)
Contact Us
info@qtpworld.com
+91- 080-42711061
Database Connections
Login | Search QTPWorld.com
What is adodb connection ?
The ADO(ActiveX Data Objects) Connection object is used to create a connection to a data source. Through this connection,
you can access and manipulate a database.
What is adodb recordset?
The ADO Recordset object is used to hold a set of records from a database table.To be able to read database data, the data
should be loaded into a recordset.
⇒QTP Scripts for connecting to MS Access:
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:mydatabase.mdb;Uid=Admin;Pwd=;"
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
'Release objects'Release objects
Set rs= nothing
Set con= nothing
Note: 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.
⇒QTP Script for connecting to sqlserver:
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open"Driver={SQL Server};server=MySqlServer;uid=MyUserName;pwd=MyPassword;database=pubs"
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
'Release objects'Release objects
Set rs= nothing
Set con= nothing
⇒QTP Script for connecting to oracle:
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open "Driver={Microsoft ODBC for Oracle};Server=QTPWorld; Uid=your_username;Pwd=your_password;"
http://www.qtpworld.com/index.php?cid=74 1/3
2. 7/22/2014 QTPWorld
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
'Release objects
Set rs= nothing
Set con= nothing
⇒QTP Script for connecting to MySQL:
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open"Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDB;User=Uname;Password=Pwd;Option=3;"
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
'Release objects
Set rs= nothing
Set con= nothing
⇒QTP Script for connecting to Excel:
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.open "DRIVER={Microsoft Excel Driver (*.xls)};DBQ=C:TestStatus.xls;Readonly=True"
rs.open "SELECT count(*) FROM [Status$] where Status = 'Failed' ",con
Msgbox rs(0)
'Release objects
Set rs= nothing
Set con= nothing
⇒QTP Script for connecting to Sybase:
Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
' Open a session to the database
con.open"Driver={SYBASE SYSTEM 11};Srvr=myServerAddress;Uid=Uname;Pwd=Pwd;Database=myDataBase;"
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
'Release objects
Set rs= nothing
Set con= nothing
http://www.qtpworld.com/index.php?cid=74 2/3
3. 7/22/2014 QTPWorld
Copyright QTPWorld.com 2013 Home | Demo videos | Students | Training | FAQ's | Feedback | About Us Designed By WebZone
http://www.qtpworld.com/index.php?cid=74 3/3