27 May 2013

How to Connec to a Database Using QTP

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:

strConName – Name of Database connection
strDSNName – DSN (Datasource Name)
strServerName – Name of Database Server
strDataBaseName – Name of Database
strUserID – Database ID
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

con=strConName

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"

Else

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

No comments:

Post a Comment