雅思 i=i+1 如果我数数的话 csvFile.写chr(34)和field.name&chr(34)和“,” elseif i=列数 csvFile.Write chr(34)&field.name&chr(34) 其他的 退出 如果结束 下一个 cs
雅思 i=i+1 如果我数数的话 csvFile.写chr(34)和field.name&chr(34)和“,” elseif i=列数 csvFile.Write chr(34)&field.name&chr(34) 其他的 退出 如果结束 下一个 csvFile.Write vbNewLine 其他的 logFile.write FormatDateTime(Now(),3)&”:数据库连接未成功。数据库连接返回代码:“&rc” logFile.write FormatDateTime(Now(),3)&):--------------脚本DailyWorkstepReport.vbs文件在“&Now()&”---------“&vbNewLine”被禁用 logFile.close csvfile.close wscript.quit 如果结束 昏暗的一排 暗柱 模糊结果数组 resultsArray=data.GetRows 暗淡的阵列 arrayRows=ubound(结果数组,2) 如果arrayRows为0,则 logFile.write FormatDateTime(Now(),3)&“&(arrayRows+1)&”行已成功读取到数组中以获取文件“&fname&vbnewline” 对于行=0到UBound(结果数组,2) 对于列=0到(UBound(resultsArray,1)-1) 如果行=Ubound(resultsArray,2)列=(Ubound(resultsArray,1)-1),则 csvFile.Write chr(34)&结果数组(列、行)&chr(34) 其他的 如果列=0,则 csvFile.Write chr(34)和formatDate(resultsArray(列、行))&chr(34)和“,” elseif列=19,则 csvFile.Write chr(34)和FormatDateTime(resultsArray(列,行),4)和chr(34)和“,” elseif列=18,则 csvFile.Write chr(34)和formatDate(resultsArray(列、行))&chr(34)和“,” elseif列=17,则 暗fDate fDate=formatDate(resultsArray(列,行))&“&FormatDateTime(resultsArray(列,行),4)&”:“&second(resultsArray(列,行))&”。&resultsArray((ubound(resultsArray,1)),行) csvFile.写chr(34)和fDate&chr(34)和“,” 其他的 csvFile.Write chr(34)和resultsArray(列、行)以及chr(34)和“,” 如果结束 如果结束 下一个 csvFile.Write vbNewLine 下一个 logfile.write FormatDateTime(Now(),3)&“&(row)&”行已写入“&fname&vbNewLine” 其他的 logFile.write FormatDateTime(Now(),3)&”:文件“&fname&vbNewLine”的查询结果数组中没有数据 logFile.write FormatDateTime(Now(),3)&):--------------脚本DailyWorkstepReport.vbs文件在“&Now()&”---------“&vbNewLine”被禁用 logfile.close csvfile.close wscript.quit 如果结束 csvFile.close logfile.write“------------DailyWorkstepReport.vbs脚本已成功结束于“&Now()&”----------------------&vbNewLine logfile.close wscript.quit REM--------------------辅助函数如下所示----------------------------- 子调试(myText) '窗口必须全局定义 '像这样调用“Debug variableName” '取消注释下一行以关闭调试 “出口接头 如果不是IsObject(对象窗口),则 Set-ObjeDebugWindow=CreateObject(“InternetExplorer.Application”) objIEDebugWindow。导航“关于:空白” objIEDebugWindow.Visible=True objIEDebugWindow.ToolBar=False 对象窗口。宽度=200 对象窗口。高度=300 对象窗口。左=10 ObjectDebugWindow.Top=10 在调试窗口时执行。忙 WScript.Sleep 100 环 objIEDebugWindow.Document.Title=“IE调试窗口” objIEDebugWindow.Document.Body.InnerHTML=“&Now&”“ 如果结束 objIEDebugWindow.Document.Body.InnerHTML=objIEDebugWindow.Document.Body.InnerHTML&myText&“雅思 i=i+1 如果我数数的话 csvFile.写chr(34)和field.name&chr(34)和“,” elseif i=列数 csvFile.Write chr(34)&field.name&chr(34) 其他的 退出 如果结束 下一个 cs,csv,datetime,vbscript,timestamp,db2,Csv,Datetime,Vbscript,Timestamp,Db2,雅思 i=i+1 如果我数数的话 csvFile.写chr(34)和field.name&chr(34)和“,” elseif i=列数 csvFile.Write chr(34)&field.name&chr(34) 其他的 退出 如果结束 下一个 csvFile.Write vbNewLine 其他的 logFile.write FormatDateTime(Now(),3)&”:数据库连接未成功。数据库连接返回代码:“&rc” logFile.write FormatDateTime(No
”&vbCrLf 端接头 函数格式化日期(sDate) 暗淡的一年 暗月 昏暗的日子 Dim格式化日期 年份=年份(sDate) theMonth=Right(字符串(2,“0”)和DatePart(“m”,sDate),2) 日期=右侧(字符串(2,“0”)和日期部分(“d”,sDate),2) formattedDate=年份和月份和日期 formatDate=formattedDate 端函数
似乎不需要写入数组,只需逐个记录读取RS记录并写入文件,占用的内存更少。将数据视为字符串而不是日期。我写入数组的原因是我需要检查最后一个值…(最后一行,最后一列)因此,我不能在该值之后写入逗号……您能解释一下我如何将日期作为字符串读入变量吗?发布您的代码和字段的数据类型。发布代码……就像您所说的,另一个选项是将其作为字符串读入记录集,但也不确定如何执行。PS不要介意我在那里调试。请把代码墙缩小到一个小范围。一个问题可能是处理第17列的(注释)语句调用函数
formatDate
,该函数只返回日期。
2016-05-04 08:00:00.000000
On Error Resume Next
Dim sPassword
Dim sUserID
Dim sDefaultLib
Dim sSystem
Dim cs
Dim rc
Dim objIEDebugWindow
sDefaultLib = *library*
sUserID = *userid*
sPassword = *password*
sSystem = *system*
cs = *connectionString*
Set con = CreateObject("ADODB.Connection")
Set data = CreateObject("ADODB.Recordset")
con.Open cs, sUserID, sPassword
rc = con.State
If (rc = 1) Then
strQuery = "SELECT * FROM Library.Table FETCH FIRST 15 ROWS ONLY FOR READ ONLY WITH UR"
data.CursorLocation = adUseClient
data.Open strQuery, con
Set filsSysObj = CreateObject("Scripting.FileSystemObject")
Dim theYear
Dim theMonth
Dim theDay
Dim mDate
mDate = Date()
theYear = DatePart("yyyy", mDate)
theMonth = Right(String(2, "0") & DatePart("m", mDate), 2)
theDate = Right(String(2, "0") & DatePart("d", mDate), 2)
mDate = theYear & theMonth & theDate
Set csvFile = filsSysObj.OpenTextFile("C:\SampleFile_" & mDate & ".csv", 8, True)
columnCount = data.Fields.Count
Set i = 0
For Each field In data.Fields
i= i + 1
If (i <> columnCount) Then
csvFile.Write Chr(34) & field.Name & Chr(34) & ","
Else
csvFile.Write Chr(34) & field.Name & Chr(34)
End If
Next
csvFile.Write vbNewLine
End If
rowCount = data.RecordCount
row = 0
Dim row
Dim column
Dim resultsArray
Dim dateArray
resultsArray = data.GetRows
debug "hi"
i = 0
Do Until i>5
MsgBox(i)
i = i + 1
'debug "in"
'Dim value
'Dim dArray()
'debug "in"
'value = Chr(34) & CStr(data.Fields(17).Value) & Chr(34) & ","
'dArray = additem(dArray, value)
'data.MoveNext
'dateArray = dArray
Loop
debug "out"
For row = 0 To UBound(resultsArray, 2)
For column = 0 To UBound(resultsArray, 1)
If row = UBound(resultsArray, 2) And column = UBound(resultsArray, 1) Then
csvFile.Write Chr(34) & resultsArray(column, row) & Chr(34)
Else
If column = 0 Then
csvFile.Write Chr(34) & formatDate(resultsArray(column, row)) & Chr(34) & ","
ElseIf column = 19 Then
csvFile.Write Chr(34) & FormatDateTime(resultsArray(column, row),4) & Chr(34) & ","
ElseIf column = 18 Then
csvFile.Write Chr(34) & formatDate(resultsArray(column, row)) & Chr(34) & ","
'ElseIf column = 17 Then
'csvFile.Write Chr(34) & formatDate(resultsArray(column, row)) & Chr(34) & ","
Else
csvFile.Write Chr(34) & resultsArray(column, row) & Chr(34) & ","
End If
End If
Next
csvFile.Write vbNewLine
Next
csvFile.close
'----------------------Helper Functions are below-----------------------------
Sub Debug(myText)
'Dim objIEDebugWindow must be defined globally
'Call like this "Debug variableName"
'Uncomment the next line to turn off debugging
'Exit Sub
If Not IsObject(objIEDebugWindow) Then
Set objIEDebugWindow = CreateObject("InternetExplorer.Application")
objIEDebugWindow.Navigate "about:blank"
objIEDebugWindow.Visible = True
objIEDebugWindow.ToolBar = False
objIEDebugWindow.Width = 200
objIEDebugWindow.Height = 300
objIEDebugWindow.Left = 10
objIEDebugWindow.Top = 10
Do While objIEDebugWindow.Busy
WScript.Sleep 100
Loop
objIEDebugWindow.Document.Title = "IE Debug Window"
objIEDebugWindow.Document.Body.InnerHTML = "<b>" & Now & "</b></br>"
End If
objIEDebugWindow.Document.Body.InnerHTML = objIEDebugWindow.Document.Body.InnerHTML & myText & "<br>" & vbCrLf
End Sub
Function formatDate(sDate)
Dim theYear
Dim theMonth
Dim theDay
Dim formattedDate
theYear = Year(sDate)
theMonth = Right(String(2,"0") & DatePart("m", sDate),2)
theDay = Right(String(2,"0") & DatePart("d", sDate),2)
formattedDate = theYear & "-" & theMonth & "-" & theDate
formatDate = formattedDate
End Function
if(rc = 1) then
logFile.write FormatDateTime(Now(), 3) & ": Database connection successful" & vbNewLine
logFile.write FormatDateTime(Now(), 3) &": Default Library: " & sDefaultLib & vbNewLine
logFile.write FormatDateTime(Now(), 3) & ": Signed into server as: " & sUserID & vbNewLine
logFile.write FormatDateTime(Now(), 3) & ": System: " & sSystem & vbNewLine
strQuery = "SELECT ws_date, groupcd, userid, firstname, lastname, clientcd, unitcd, categorycd, category, activity, wrktype, subwrktype, step_begin, step_end, report_indicator, report_indicator, count, event_dattim, key_date, key_time, key_milsec, microsecond(event_dattim) FROM *Library.Name* FOR READ ONLY WITH UR"
data.CursorLocation = adUseClient
data.open strQuery, con
if data.EOF then
logFile.write FormatDateTime(Now(), 3) & ": The query returned no data"
logFile.write FormatDateTime(Now(), 3) & ": ---------------- The script DailyWorkstepReport.vbs file was abended at " & Now() &". There was no worksteps file created. ----------------" & vbNewLine
logFile.close
end if
columnCount = data.Fields.Count
columnCount = columnCount - 1
Set filsSysObj = CreateObject("Scripting.FileSystemObject")
Set csvFile = filsSysObj.OpenTextFile("C:\VBScript\Dailys\" & fname, 8, True)
set i = 0
for each field in data.Fields
i= i + 1
if i < columnCount then
csvFile.Write chr(34) & field.name & chr(34) & ","
elseif i = columnCount then
csvFile.Write chr(34) & field.name & chr(34)
else
exit for
end if
next
csvFile.Write vbNewLine
else
logFile.write FormatDateTime(Now(), 3) & ": Database connection was unsuccessful. Database Connection Return Code: " & rc
logFile.write FormatDateTime(Now(), 3) & ": ---------------- The script DailyWorkstepReport.vbs file was abended at " & Now() &". ----------------" & vbNewLine
logFile.close
csvfile.close
wscript.quit
end if
dim row
dim column
dim resultsArray
resultsArray = data.GetRows
dim arrayRows
arrayRows = ubound(resultsArray, 2)
if arrayRows <> 0 then
logFile.write FormatDateTime(Now(), 3) & ": " & (arrayRows + 1) & " rows were successfully read into the array for file " & fname & vbnewline
for row = 0 to UBound(resultsArray, 2)
for column = 0 to (UBound(resultsArray, 1) - 1)
if row = Ubound(resultsArray, 2) and column = (ubound(resultsArray, 1) - 1) then
csvFile.Write chr(34) & resultsArray(column, row) & chr(34)
else
if column = 0 then
csvFile.Write chr(34) & formatDate(resultsArray(column, row)) & chr(34) & ","
elseif column = 19 then
csvFile.Write chr(34) & FormatDateTime(resultsArray(column, row),4) & chr(34) & ","
elseif column = 18 then
csvFile.Write chr(34) & formatDate(resultsArray(column, row)) & chr(34) & ","
elseif column = 17 then
Dim fDate
fDate = formatDate(resultsArray(column, row)) & " " & FormatDateTime(resultsArray(column, row),4) & ":" & second(resultsArray(column,row)) & "." & resultsArray((ubound(resultsArray, 1)), row)
csvFile.Write chr(34) & fDate & chr(34) & ","
else
csvFile.Write chr(34) & resultsArray(column, row) & chr(34) & ","
end if
end if
next
csvFile.Write vbNewLine
next
logfile.write FormatDateTime(Now(), 3) & ": " & (row) & " rows have been written to " & fname &vbNewLine
else
logFile.write FormatDateTime(Now(), 3) & ": There was no data in the query results array for file " & fname & vbNewLine
logFile.write FormatDateTime(Now(), 3) & ": ---------------- The script DailyWorkstepReport.vbs file was abended at " & Now() &". ----------------" & vbNewLine
logfile.close
csvfile.close
wscript.quit
end if
csvFile.close
logfile.write "---------------- DailyWorkstepReport.vbs script successfully ended at " & Now() & "----------------" & vbNewLine
logfile.close
wscript.quit
REM ----------------------Helper Functions are below-----------------------------
Sub Debug( myText )
'Dim objIEDebugWindow must be defined globally
'Call like this "Debug variableName"
'Uncomment the next line to turn off debugging
'Exit Sub
If Not IsObject( objIEDebugWindow ) Then
Set objIEDebugWindow = CreateObject( "InternetExplorer.Application" )
objIEDebugWindow.Navigate "about:blank"
objIEDebugWindow.Visible = True
objIEDebugWindow.ToolBar = False
objIEDebugWindow.Width = 200
objIEDebugWindow.Height = 300
objIEDebugWindow.Left = 10
objIEDebugWindow.Top = 10
Do While objIEDebugWindow.Busy
WScript.Sleep 100
Loop
objIEDebugWindow.Document.Title = "IE Debug Window"
objIEDebugWindow.Document.Body.InnerHTML = "<b>" & Now & "</b></br>"
End If
objIEDebugWindow.Document.Body.InnerHTML = objIEDebugWindow.Document.Body.InnerHTML & myText & "<br>" & vbCrLf
End Sub
function formatDate(sDate)
Dim theYear
Dim theMonth
Dim theDay
Dim formattedDate
theYear = Year(sDate)
theMonth = Right(String(2,"0") & DatePart("m", sDate),2)
theDay = Right(String(2,"0") & DatePart("d", sDate),2)
formattedDate = theYear & "-" & theMonth & "-" & theDate
formatDate = formattedDate
end function