我只是想将查询结果写入.txt文件。我陷入了困境,似乎无法继续前进。
' # writes records to a .txt file
Wscript.timeout = 600
Const adLockBatchOptimistic = 4
Const adUseClient = 3
Const writeFile = "C:\Path\CallLog.txt"
Dim strWrite, strSQL, strResult
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionTimeout=30
conn.CommandTimeout=30
conn.Open "Provider=SQLOLEDB;Data Source=Server;Trusted_Connection=Yes;" & _
"Initial Catalog=Database;User ID=User;Password=Password;"
Set rs = CreateObject("ADODB.Recordset")
strSQL = "SELECT fld1, fld2, " & _
"fld3, fld4, fld5, fld6, fld7, fld8, fld9" & _
"FROM Database.dbo.[table]"
'conn.Execute strSQL
rs = strSQL, conn ' <- this line causes an error.
Open writeFile For Ouput As #1
Do Until rs.EOF
strResult = rs
Print #1, strResult
rs.MoveNext
Loop
Close #1
rs.Close
Set rs = Nothing
答案 0 :(得分:4)
此:
Open writeFile For Ouput As #1
不是VBScript。请参阅the docs以了解如何创建 - 打开txt文件。将记录集(的内容)写入文本文件的低技术标准方法是
tsOut.Write oRS.GetString(adClipString, , sFSep, sLSep, "NULL")
如果您打开了输出文件tsOut,recodset oRS和定义的adClipString(2),字段分隔符sFSep(例如“,”或vbTab)和行分隔符sLSep(例如vbCrLf)
答案 1 :(得分:0)
好的,我们将尝试不同的东西。请尝试以下代码:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set writeFile = objFSO.OpenTextFile("C:\Path\CallLog.txt", 1)
Wscript.timeout = 600
Const adLockBatchOptimistic = 4
Const adUseClient = 3
Dim strWrite, strSQL, strResult
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionTimeout=30
conn.CommandTimeout=30
conn.Open "Provider=SQLOLEDB;Data Source=Server;Trusted_Connection=Yes;Initial
Catalog=Database;User ID=User;Password=Password;"
Set rs = CreateObject("ADODB.Recordset")
strSQL = "SELECT fld1, fld2, " & _
"fld3, fld4, fld5, fld6, fld7, fld8, fld9" & _
"FROM Database.dbo.[table]"
'conn.Execute strSQL
rs.Open strSQL, conn ' <- this line causes an error.
On Error Resume Next
Do Until rs.EOF
strResult = rs
writeFile.WriteLine strResult
rs.MoveNext
Loop
rs.Close
Set rs = Nothing