Showing posts with label Date Conversion Function in UFT. Show all posts
Showing posts with label Date Conversion Function in UFT. Show all posts

Saturday, 3 March 2018

VBScript or UFT code to connect database and extract/update data

Following code can be used to connect to database to perform select/update queries.

Prerequisites:
  1. Install DB client in the machine from where we are executing this piece of code
  2. Establish DSN(Data Source Name)

Logic how VBScript code connects to DB:
DB client and WSH engine creates the interfaces/platforms separately but to make the bridge between both, we should have DSN which works as communication channel to perform the transactions.

strServerName= “abcd.com”
strDBName= “database”
strDBUserName="DBUserName"
strDBPWD= “DBPWD"
strSQL=  "Select * from employee "
If rc = micPass Then rc = DB_Execute_Query(strSQL,strServerName,strDBName,strDBUserName,strDBPWD)


'*******************************************************************************
'Function Name    : DB_Connection
'Description      : Function to establish the connection to database
'Input Parameters : strDataBaseUser,strDBPassword,strDataBaseName
'Output Parameters: None
'Createb By: Govardhan
'********************************************************************************


Public Function DB_Connection(strServerName,strDBName,strDBUserName,strDBPWD)
On Error Resume Next

Set Conn = CreateObject ("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
''' 'Create the string which is the command for connection
'''' strCon="Driver={PostgreSQL ANSI};Server=abc.com;Port=5432;Database=database;UID=server;PWD=server;"

strCon="Driver={PostgreSQL ANSI};Server="&strServerName&";Port=5432;Database="&strDBName&";UID="&strDBUserName&";PWD="&strDBPWD
Conn.ConnectionString = strCon
Conn.Open()
If Err.Number <> 0 then
rc = micFail
Reporter.ReportEvent rc,"Connect to database","Unable to connect to database due to following error," &_
vbcrlf & "Error Code: " & Err.Number &_
vbcrlf & "Error Description: " & Err.Description
Err.Clear
Else
rc = micPass
Reporter.ReportEvent rc,"Connect to database - "&strDBName &" - was successful","Connection successful"

Environment("DB_Connection_"&strDBName)= Conn
Environment("DB_Recordset_"&strDBName)= rs
End If
On Error GoTo 0
DB_Connection = rc
End Function

'*******************************************************************************
'Function Name    : DB_Execute_Query
'Description      : Function to execute a query and check if successfully executed or not
'Input Parameters : strSql,strDataBaseUser,strDBPassword,strDataBaseName
'Output Parameters: 0- Error,1 - Successful
'********************************************************************************

Public Function DB_Execute_Query(strSql, strServerName,strDBName,strDBUserName,strDBPWD)

On Error Resume Next

DB_Connection strServerName,strDBName,strDBUserName,strDBPWD
Set Conn = Environment("DB_Connection_"&strDBName)
Set rs = Environment("DB_Recordset_"&strDBName)  
   
Set rs = Conn.Execute (strSql)

If Err.Number <> 0 then

Reporter.ReportEvent micFail,"Query the database","Following error while querying the database," &_
vbcrlf & "Sql Statement: " & strSql &_
vbcrlf & "Error Code: " & Err.Number &_
vbcrlf & "Error Description: " & Err.Description
Err.Clear
rc  = micFail
DB_Execute_Query = rc
Else
rc = micPass
Reporter.ReportEvent micPass,  "Execute Sql Query","User should able to execute the sql query and the query is" & strSql ," Query Executed succefully"
DB_Execute_Query = rc
End If

'Close Connection and Recordset objects
DB_Close Conn,rs
On Error GoTo 0
DB_Execute_Query = rc
End Function

'*******************************************************************************
'Function Name    : DB_Close
'Description      : Function to disconnect the DB connection
'Input Parameters : strDataBaseName,RecordSet 
'Output Parameters: None
'********************************************************************************

Public Function DB_Close(strConnection,StrRecordSet)
On Error Resume Next

Set StrRecordSet= nothing
strConnection.close
    Set strConnection = nothing

On Error GoTo 0

End Function

Friday, 20 November 2015

Converting Date Format with VBScript in UFT

Function ConvertDate(ByVal sDate, ByVal sFormat)
   Dim d,m,y,NewDate

    d=Day(sDate)
    m=Month(sDate)
    y=Year(sDate)

    If Len(d)=1 Then d = "0" & d
    If Len(m)=1 Then m = "0" & m

    Select Case sFormat
        Case "DD-MM-YYYY"
                    NewDate = d & "-" & m & "-" & y
        Case "DD/MM/YYYY"
                    NewDate = d & "/" & m & "/" & y
        Case "MM/DD/YYYY"
                    NewDate = m & "/" & d & "/" & y
        Case "DD-MON-YYYY"
                    Select Case CInt(m)
                    Case "1" mm = "Jan"
                    Case "2" mm = "Feb"
                    Case "3" mm = "Mar"
                    Case "4" mm = "Apr"
                    Case "5" mm = "May"
                    Case "6" mm = "Jun"
                    Case "7" mm = "Jul"
                    Case "8" mm = "Aug"
                    Case "9" mm = "Sep"
                    Case "10" mm = "Oct"
                    Case "11" mm = "Nov"
                    Case "12" mm = "Dec"
                    End Select
                   
                    NewDate = d & "-" & mm & "-" & y
    End Select
   
    ConvertDate = NewDate
End Function