2015-06-03 55 views
0

我有這個Asp Classic 3.0代碼的問題。Asp經典3.0循環和movenext

我需要發送電子郵件時中的SQL查詢的數據庫表中找到與記錄:

SQL = " SELECT * FROM doTable Where div = 1; " 

我試圖此ASP代碼:

SQL = " SELECT * FROM doTable Where div = 1; " 

    Set Rec = createObject("ADODB.Recordset") 
    Rec.open SQL, cn 

    If not Rec.eof then 

    msg = msg & VBcrlf & "<br />Records founds!<br />" 

    Do while not Rec.eof 

    msg = msg & VBcrlf & "ID record: " & Rec("Id") & ""   

    msg = "" 

    Rec.moveNext()  
    Loop  

    Else 

    msg = msg & VBcrlf & "<br />No records!<br />" 

    End If 

    Rec.close() 
    set Rec = nothing  

    cn.close() 
    set cn = nothing 

問題是這部分代碼:

msg = "" 

如果我發現記錄和味精= 「」存在於代碼中,msg的輸出爲空;如果味精=「」沒有出現在代碼的輸出是:

Records founds! 
ID record: 32 
Records founds! 
ID record: 61 
Records founds! 
ID record: 77 

爲什麼我不能得到這個輸出?

Records founds! 
ID record: 32 
ID record: 61 
ID record: 77 

你能幫幫我嗎? 感謝您在高級 -

編輯

With objMessage 
    .From  = RS("Email_from") 
    .To  = RS("Email_to") 
    .Subject = "Alert div" 
    .HtmlBody = msg 
    .Fields("urn:schemas:httpmail:importance").Value = 2 
    .Fields("urn:schemas:mailheader:X-MSMail-Priority") = 6 
    .Fields.Update()  

on error resume next 
    .Send 
    if Err.Number <> 0 then 
     response.Write "Email send failed # : " & Err.Number & " - " & Err.Description & ".<br />"&vbcrlf 
    end if 

End With 

回答

0
msg = msg & VBcrlf & "ID record: " & Rec("Id") & ""   

msg = "" 

這個代碼是沒有意義的,msg = ""味精的值更改爲空字符串,並使得前行毫無意義。在開始循環記錄集之前,開始msg = ""會更有意義。

試一下這個

SQL = " SELECT * FROM doTable Where div = 1; " 

msg = "" 

msg = msg & VBcrlf & "<br />Records founds!<br />" 


    Set Rec = server.createObject("ADODB.Recordset") 
    Rec.open SQL, cn 

    If Rec.eof and rec.bof then 

    msg = "<br />No records!<br />" 

    else 

    Do while not Rec.eof 

    msg = msg & VBcrlf & "ID record: " & Rec("Id") & ""   

    Rec.moveNext  
    Loop  

    End If 

    Rec.close 
    set Rec = nothing  

    cn.close 
    set cn = nothing 
+0

非常感謝你! –

+0

以這種方式字符串「Records founds!」總是出現,如果沒有記錄。 –

+0

@FabioPellerito - 我不這麼認爲。 'msg =「
沒有記錄!
如果記錄集爲空,則應覆蓋」找到記錄「。 – John

0

我認爲代碼是正確的。在哪裏輸出變異msg

Records founds! 
ID record: 32 
Records founds! 
ID record: 61 
Records founds! 
ID record: 77 

也許這是3查詢的輸出?

+0

不是... 1個查詢的輸出...在第一個問題**編輯**我發佈了變量味精,謝謝 –

+0

我想知道你在哪裏放置發送電子郵件的代碼行。進入或退出循環。 –

1

我想你在找什麼是Rec.recordcount並可以這樣使用:

Set Rec = server.createObject("ADODB.Recordset") 
Rec.open SQL, cn 
somevar=Rec.recordcount 

然後somevar可用於顯示是這樣的:

There are <%=somevar%> records.