Wednesday, 6 May 2015

QTP - 8 -How to connect to a database ?

Before going to database , lets see two important concepts:

-> 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.

-> 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.


To coonect to a database we need the following to be done :

1) create an instance of the database - here we create a object of the database
Eg :
set abc = Createobject("ADODB.Connection")

2) Create a connection string - It proides a string to connect to a particular database. It varies from database to database. Its usually provided by developer.
Connection string consist of data source and data provider. Datasource is the name of the database and provider is the driver of the database.

3) Create a record set - Its used to read the values from the tables. To fetch any data we need to create object of recordset

Eg  :
Set Rs = Createobject("ADODB.Connection")


4) Select the values by writing the queries.In record set we write queries to fetch data from database.
rs.Open "Select * from Table",oconn

5) use loop to display the data
Do while not Rs.EOF
' EOF hecks till end of data in recordset.
user =Rs.Fields("Username")
Pass =Rs.fields("Password")

'Here fields are used to get the data against each column, we give the olumn name

6) Clear the objects created :
Set Rs = Nothing
set Conn = Nothing

This is used to clear the llocated memory to the object.

-------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment