Connect to Database Using VB Script, VB Script Database Connection example in QTP



Connect to Database Using VB Script, VB Script Database Connection example in QTP



How would you connect to database using vb script ?
You must know the following To connect to the database:-
a) Server connection string
b) user name
c) Password
d) Name of DNS
Now We can build the code for database connectivity command directly or We can also use the SQL Query tool given by the QTP.

QTP Script provided below is the normal example of connecting the QTP and SQL server. For an example We have generated a simple table with 3 columns as displayed below. The script of QTP is displaying the second column values one by one in the message box. Script provided below never forgot to implement the own user name, server name, database, password etc.

Option Explicit
Dim conn,rss
'The ADO Connection Object simply use to create an open connection to a data source.

Set conn=createobject("adodb.connection")

'The ADO Record-set object is used to hold a set of 
records from a database table. 
'A Record-set object consist of columns and records (fields).
 When you first open a Record-set, 
'the current record pointer will point to the first record 
and the EOF and BOF properties are False. 
'If there are no records, the EOF and BOF property are True.

Set rss=createobject("adodb.recordset")
'Below it displays the  server, provider, password, user name, database
conn.open "provider=sqloledb.1; server=server2010; uid=rts_dev1; 
pwd=password; database=model"

'See all the parameters to record-set "Open" commands will be 
- http://www.w3schools.com/ado/met_rs_open.asp

rss.open "select * from persons",conn
Do while not rss.eof
'It show the SECOND column value    
      msgbox rss.fields.Item(1)   
      rss.MoveNext 
Loop
conn.Close

Connect to Database Using VB Script
Connect to Database Using VB Script

VB Script Database Connection example
VB Script Database Connection example




















Virtual Objects with Example in QTP
Cross Platform Testing


What is Object Spy in QTP Ordinal Identifiers Alone can Make an Object Unique



Test Automation Frameworks Types



No comments:

Post a Comment

Popular Posts