2015-06-03 58 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.