10 Dec 2012

Database Connections


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

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

QTP Script for connecting to oracle

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

QTP Script for connecting to MySQL

Dim dbexample
Set dbexample=CreateObject("ADODB.Connection")

dbexample.ConnectionString="driver=mysql;server=localhost;uid=uuuu;pwd=pppp;database=db123"
dbexample.Open

Set recordset=dbexample.execute("Select * from mytable")
msgbox recordset.getString

QTP Script for connecting to Sybase

Create the Connection Object and Recordset Object
Set objDB = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

' Open a session to the database
objDB.ConnectionString="Driver={SYBASE Driver};NA=host,port;DB=name;Uid=user;Pwd=psw;"
objDB.Open

' Run the SQL query
ObjRecordset.Source="select * from ..."
ObjRecordset.ActiveConnection=objDB
objRecordset.Open
intRows = objRecordset.recordcount
msgbox(intRows)
msgbox(objRecordset("").Value)

' Clean up and close the database session
objDB.Close
Set objDB = Nothing

No comments:

Post a Comment