I have a VBScript file that extracts data from a SQL Server database table and then write the result in a CSV file. My problem is that each record gets written to the file one line at a time. I would like the recordset to be pasted into the CSV file in a similar way when using CopyFromRecordset
in Excel VBA. Is there a way to do this? Here is my current code that reads the recordset one line at a time,
Set con = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set outFile = CreateObject("Scripting.FileSystemObject").CreateTextFile("C:\Users\JASON SAMUELS\Desktop\VBS\test.csv")
con.Open "Provider=SQLOLEDB.1;Password=pwd123;Persist Security Info=True;User ID=TEST;Initial Catalog=DemoDB;Data Source=JASONPC\SQLEXPRESS"
rs.Open "SELECT * FROM MyTable", con, 1,3
rs.MoveFirst
Do
'WScript.Echo rs("Name") & " " & rs("Surname")
outFile.WriteLine rs("Name") & "," & rs("Surname")
rs.MoveNext
Loop Until rs.EOF
rs.close
con.Close
Set con = Nothing