VB脚本ADODB.Stream创建无限while循环

时间:2014-03-24 14:58:14

标签: vbscript

我有vbs文件。它将从sql server 2000数据库中获取记录。 使用do while循环, 然后它从记录集中提取数据列 使用steam对象,创建文件&写下zip文件的内容 但我得到了无限循环。

请查找样本的数据库记录

File_id File_Name File_content(blob datatype)

23127376 File_1 afdfasdf253asdf6asdf52asd45fasf

23127377 File_2 afdfasdf253asdf6asdf52asd45fasf

23127378 File_3 afdfasdf253asdf6asdf52asd45fasf

23127379 File_4 afdfasdf253asdf6asdf52asd45fasf

23127380 File_5 afdfasdf253asdf6asdf52asd45fasf

23127381 File_6 afdfasdf253asdf6asdf52asd45fasf

23127382 File_7 afdfasdf253asdf6asdf52asd45fasf

23127383 File_8 afdfasdf253asdf6asdf52asd45fasf

23127384 File_9 afdfasdf253asdf6asdf52asd45fasf

23127385 File_10 afdfasdf253asdf6asdf52asd45fasf

23127386 File_11 afdfasdf253asdf6asdf52asd45fasf

23127387 File_12 afdfasdf253asdf6asdf52asd45fasf

23127388 File_13 afdfasdf253asdf6asdf52asd45fasf

23127389 File_14 afdfasdf253asdf6asdf52asd45fasf

23127390 File_15 afdfasdf253asdf6asdf52asd45fasf

23127391 File_16 afdfasdf253asdf6asdf52asd45fasf

请查看以下代码

DO
     Set lobjStream = CreateObject("ADODB.Stream")
     lobjStream.Mode = 3 ' adModeReadWrite   3       Read/write.
     lobjStream.Type = 1 ' adTypeBinary      1       Binary data
     lobjStream.Open
     SDFMB02_FILE_ID_K = ObjRS("SDFMB02_FILE_ID_K")
     logMessage = "[Record] File ID (SDFMB02_FILE_ID_K) :: " & SDFMB02_FILE_ID_K
     logFilte.WriteLine (logMessage)
     lobjStream.Write ObjRS("DFMB02_FILE_BLOB_I").Value 
     lobjStream.SaveToFile ZipPath & ObjRS("DFMB02_FILE_N").Value, 2    
     ZipFile = ZipPath & ObjRS("DFMB02_FILE_N").Value       
     ZIPFldPath = Left(ObjRS("DFMB02_FILE_N").Value, Len(ObjRS("DFMB02_FILE_N").Value) - 4)

     ObjRS.movenext
     set lobjStream =  Nothing
Loop While ObjRS.EOF = False

过了一段时间,我会得到无限循环。请查找此代码的日志。

[Record] File ID (SDFMB02_FILE_ID_K) :: 23127376
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127377
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127378
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127379 
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127380
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127381
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127382
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127383
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127384
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127385
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127385
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127385
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127385
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127385
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127385
[Record] File ID (SDFMB02_FILE_ID_K) :: 23127385

请你帮忙解决这个问题

1 个答案:

答案 0 :(得分:1)

您应该正确处理Do语句中的EOF而不是While语句。你可以通过检查上次保存的值来打破循环。

Dim lastsaved : lastsaved = ""
Do While Not ObjRS.EOF

    Set lobjStream = CreateObject("ADODB.Stream")
    lobjStream.Mode = 3 ' adModeReadWrite   3       Read/write.
    lobjStream.Type = 1 ' adTypeBinary      1       Binary data
    lobjStream.Open
    Dim check : check = ObjRS("SDFMB02_FILE_ID_K")
    If lastsaved = check Then
        Exit Do
    Else
        lastsaved = check
        SDFMB02_FILE_ID_K = check
    End If
    logMessage = "[Record] File ID (SDFMB02_FILE_ID_K) :: " & SDFMB02_FILE_ID_K
    logFilte.WriteLine (logMessage)
    lobjStream.Write ObjRS("DFMB02_FILE_BLOB_I").Value 
    lobjStream.SaveToFile ZipPath & ObjRS("DFMB02_FILE_N").Value, 2    
    ZipFile = ZipPath & ObjRS("DFMB02_FILE_N").Value       
    ZIPFldPath = Left(ObjRS("DFMB02_FILE_N").Value, Len(ObjRS("DFMB02_FILE_N").Value) - 4)

    ObjRS.movenext
    Set lobjStream =  Nothing
Loop