I really appreciate if some one is willing to shade some light on this.
I wanted to write a function using VBScript (Qtp's preferred Lang )
Connect to a sql server using applicable credentials and run a Query,in which the result can be accessed through ADO DB object - recordset and later save to an csv file.
My biggest challenge is to create a .CSV file in C:\ drive and save the Query result to the CSV file. using vb script on QTP HP Tool.
code
*************************************************************************************************
Function RecordsetToCSV()
str_query = "SELECT top 20 A.BusinessID, A.dbaName, DM.Tier ,DM.Yield, DM.Frequency FROM T2Businesses A" _
& " LEFT OUTER JOIN TOptionList OL ON A.Status = OL.Value AND OL.ListName = 'DealerDeveloperStatus'" _
& " LEFT OUTER JOIN TDealerMetrics DM ON A.BusinessID = DM.BusinessID" _
& " LEFT OUTER JOIN TEntity E ON A.BusinessID = E.EntityID" _
& " WHERE E.isdeleted=0 and (A.IsVAR = 1 or (A.IsVAR = 1 OR A.IsPOSDeveloper = 1) or A.IsPOSDeveloper = )" _
& " and DM.Yield is not null order by A.dbaName desc"
Set objdbconnection = CreateObject("ADODB.Connection")
Set rsTemp = CreateObject("ADODB.Recordset")
strdbserver = "xyz.prod.qa.dev"
strdbuserId = "QTP"
strdbpwd = "P@ssword!"
strdbname = "dbm_database"
objdbconnection.ConnectionString = "DRIVER=SQL Server;SERVER=" & strdbserver & ";Initial Catalog=" & strdbname & " ;User Id=" & strdbuserId & ";Password=" & strdbpwd & ";"
objdbconnection.Open , ConnectionString
rsTemp.Open str_query, objdbconnection, adOpenStatic, adLockOptimistic
CSVData = DataToCSV(rsTemp)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.Open "C:\MyFileName.csv" For Binary Access Write As #1
Put #1, , CSVData
Close #1
rsTemp.Close
Set rsTemp = Nothing
End Function
Public Function DataToCSV(rsData )
If ShowColumnNames Then
For K = 0 To rsData.Fields.Count - 1
RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"
Next
RetStr = Mid(RetStr, 2) & vbNewLine
End If
RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)
RetStr = Left(RetStr, Len(RetStr) - 3)
DataToCSV = RetStr
End Function
Thanks,
James.