Connecting to a Database Using QTP

The below function can be used to connect to any database. The function establishes DSN Connection. Below are more details:

‘1. strConName – Name of Database connection
‘2. strDSNName – DSN (Datasource Name)
‘3. strServerName – Name of Database Server
‘4. strDataBaseName – Name of Database
‘5. strUserID – Database ID
‘6. strPassword- Database Password

Function ConnectDatabase(Byval strConName,Byval strDSNName,Byval strServerName,Byval strDataBaseName,Byval strUserID,Byval strPassword)
MsgBox "Inside 'ConnectDatabase' Function"
On Error Resume Next
'Assign Database Connection Information to the DB Variables
DataSource = strDSNName
Server = strServerName
UserID_DB = strUserID
Password_DB = strPassword
DatabaseName_DB = strDataBaseName
Dim connItems(2)
Dim connState
'Declare Database Recordset variable
conString = cstr("DSN="&DataSource&chr(59)&"Server="&Server&chr(59)&"UID="&UserID_DB&chr(59)&"Password="&Password_DB&chr(59)&"DATABASE="&DatabaseName_DB)
'Create Database Connection Object
Set conn = CreateObject("Adodb.Connection")
'Open Database Connection
conn.Open conString
'Verify the Successful Database Connection Establishment
If conn.State=1 Then
Reporter.ReportEvent micPass,"Database Connection Success"," Database connection for "&DatabaseName_DB &"Database has been established Successfully"
'MsgBox "Connection Estblished Successfully"
Reporter.ReportEvent micFail,"Database Connection Failure"," Database connection for "&DatabaseName_DB &"Database is not been established successfully"
'MsgBox "Connection Not Estblished"
End If
connItems(0) = conn.State
connItems(1) = conn
ConnectDatabase = connItems
End Function

By shalabhdixit

2 comments on “Connecting to a Database Using QTP

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s