我使用VBscript从Oracle数据库中提取数据,并将记录集结果放入csv文件中。这是我的代码,
Set con = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set ssfile = CreateObject("Scripting.FileSystemObject").CreateTextFile("C:\Users\jasons\Documents\Closing_stock\scripts\SuperSession.csv")
con.Open "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=athena)(PORT=1521)))(CONNECT_DATA=(SID=jasdnf)));User Id=xxxx;Password=xxxx"
rs.Open "select * from mytable", con, 1, 3
ssfile.WriteLine "ITEM, ALTITEM"
rs.MoveFirst
Do
ssfile.WriteLine rs("item") & "," & rs("altitem")
rs.MoveNext
Loop Until rs.EOF
rs.Close
con.Close
Set con = Nothing
代码工作正常。我遇到的问题是,大约有400万条记录,需要很长时间才能遍历每条记录并将其粘贴到csv文件中。 VBscript中是否有与Excel-VBA类似的功能,例如CopyFromRecordset,其中整个记录集可以转储到工作表中。所以我想将记录集结果转储到csv文件中,而不必遍历每条记录。我怎么能这样做?
答案 0 :(得分:0)
您可以尝试使用GetString()
,它会将记录集的所有(或可选的子集)作为字符串返回,您可以在其中指定分隔符等:
Set con = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set ssfile = CreateObject("Scripting.FileSystemObject").CreateTextFile("C:\Users\jasons\Documents\Closing_stock\scripts\SuperSession.csv")
con.Open "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=athena)(PORT=1521)))(CONNECT_DATA=(SID=jasdnf)));User Id=xxxx;Password=xxxx"
rs.Open "select * from mytable", con, 1, 3
ssfile.WriteLine "ITEM, ALTITEM"
rs.MoveFirst
ssfile.writeline rs.GetString(2, , ",")
rs.Close
con.Close
Set con = Nothing
不确定是否需要提供rowdelimiter,您可能需要进行试验 - 例如尝试rs.GetString(2, , ",", vbNewLine)