QTP Database Connection

HP QTP/UFT have a privileges to connect Database in order to perform database testing .VB Script is object based language and it is not support external files directly in QTP ,if want to work with outside files we have to create objects for those files in order to work with external files.Mostly external files are Database , Text Files , Excel files , CSV files , Word and PDF files.

Syntax:

Set objectname = CreateObject("Server name.Class name")

Below are the Database Classes


  1. Connection
  2. Command
  3. Recordset


1.Connection:


Connection class in QTP/UFT is helpful in establish a connection to particular Database during runtime.

Syntax:

Set objconn = Createobject("ADODB.connection")
objconn.open "provider.provide name"

What is ADODB?

The ADODB(ActiveX Data Object Data Base) is a connection object which is helping to connect to a data source,using this connection can connect to the database.

Examples:

1.MS Access

objconn.open "provider=microsoft.jet.OLEDB.4.0.Data source = Path of Database"

2.SQl Server:

objconn.open "provider=sqloledb.1;server=ip address;uid=username;pwd=xxxx;database=database name"

3.Oracle:

objconn.open "provider=oraoledb.1;server=servername;userid=username;password=password"

OLEDB - Object Linking Embedded Database.

How to create Provider Name:



  1. Save empty notepad as .udl extension
  2. Open udl file
  3. Click on Provider tab
  4. Select Provider
  5. Click on Next
  6. Select Database name
  7. Click on Test Connection
  8. Click on OK
  9. Click on OK
  10. Open udl file in Notepad and you will get Provider Name details in script.


2.Command


Command class used to execute particular DML statements(Insert,Update,Delete etc) on connected Database.

Syntax:

Set objcomm = Createobject("ADODB.command")
objcomm.ActiveConnection = objconn
objcomm.commandtext="Sql Statement"

3.Recordset


Recordset class helpful in execute particular "Select" statements on connected database and retrieves the results.

Syntax:

Set objrs = createobject("ADODB.Recordset")
objrs.open "Sql statement",objcon

Nothing:


Nothing is a keyword in VB script,where it is used to dis associate an object variable from actual objects.

Syntax:

Set object name = nothing


How to set data into database

Option Explicit
Dim Objconn,objcomm

Set Objconn = Createobject("ADODB.Connection")
Objcon.open "provider=provider name"
Set Objcomm = CreateObject("ADODB.Command")
Objcomm.Activeconnection = Objconn
Objcomm.commandtext = "Insert into Mst_Store values('rajesh food hall',5412)"
Objcomm.Execute
Objcomm.Close

Set Objcomm = nothing
Set Objconn = nothing

Post a Comment

0 Comments