Book Image

Advanced UFT 12 for Test Engineers Cookbook

Book Image

Advanced UFT 12 for Test Engineers Cookbook

Overview of this book

Table of Contents (18 chapters)
Advanced UFT 12 for Test Engineers Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Establishing and closing a database connection


In this recipe, we will show how to establish a DB connection using VBScript code. We will build a simple custom class, DB_Handler that will be instantiated using a global scope variable, oDBHandler. This global object will serve as the basis for all our DB operations.

Getting ready

From the File menu, navigate to New | Function Library... or use the Alt + Shift + N shortcut. Name the new function library DB_Func.vbs.

How to do it...

The following code handles creating, opening, and closing a DB connection using ADODB:

Const C_ADODB_OBJ = "ADODB.Connection"
Dim oDBHandler

Function createDBHandler(p, ds, ic, uid, pwd)
    On error resume next
    Set oDBHandler=new DB_Handler
    
    call oDBHandler.Init(p, ds, ic, uid, pwd)
    
    createDBHandler=eval("err.number=0")    
End Function

Class DB_Handler
    private m_oDBConnection
    Public Provider
    Public DataSource
    Public InitialCatalog
    Public Username
    Public Password
    
 ...