1

我只是想将查询结果写入 .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
4

2 回答 2

4

这个:

Open writeFile For Ouput As #1 

不是 VBScript。请参阅文档以了解如何创建打开 txt 文件。将记录集(的内容)写入文本文件的低技术标准方法是

tsOut.Write oRS.GetString(adClipString, , sFSep, sLSep, "NULL")

这正是要搞砸的一行——前提是您打开了输出文件 tsOut、recodset oRS,并定义了 adClipString (2)、字段分隔符 sFSep(例如“,”或 vbTab)和行分隔符 sLSep(例如 vbCrLf)。

于 2013-07-18T15:12:29.110 回答
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
于 2013-07-18T14:49:48.067 回答