带有SQL数据库返回的Asp Classic填充表

时间:2016-02-11 17:54:59

标签: sql asp-classic

我有ASP经典代码和SQL语句,它返回两列数据,我需要填充一个表。

我调用了下面的函数,它连接到数据库,但挑战在于如何将返回的数据反馈到表中。我可能需要做某些事情"做什么时候",这是我被卡住的地方,我可以使用一些指示或指导:)

Function FillTable()

    Msg_Body = Msg_Body & "
" & vbCrLf
    Msg_Body = Msg_Body & "" & vbCrLf
    Msg_Body = Msg_Body & "" & vbCrLf    
   sql = " Select distinct a.column1 as ColumnName, count(a.column1) as TotalCount " & vbCrLf
   sql = sql & " from table1 a, table2 c, table1_type d, sprt_sc_folder e  " & vbCrLf
   sql = sql & " WHERE a.column1 = b.column2 " & vbCrLf
   sql = sql & " group by a.column1 " & vbCrLf
   response.write("Column_Count:
" & sql & "

")
    set rs = connP15.Execute(sql)

    Column_Name = rs("ColumnName")
    Total_Count = rs("TotalCounts")

End Function

1 个答案:

答案 0 :(得分:1)

您想将此表放入Msg_Body,还是要将其显示在屏幕上,或者您希望函数将表格作为其值返回,还是......?

Function FillTable()
    dim t
   '...
    set rs = connP15.Execute(sql)
    t = "<table><tr><th>Column</th><th>Count</th></tr>"
    Do Until rs.EOF
        t = t & "<tr>"
        t = t & "<td>" & rs("ColumnName") & "</td>"
        t = t & "<td>" & rs("TotalCount") & "</td>"
        t = t & "</tr>"
        rs.Movenext '<-- very, very important! (well, unless you like hung servers)
    Loop
    t = t & "</table>"
    rs.Close
    Set rs = Nothing
    FillTable = t
End Function

而不是Do...Loop,您可以尝试使用rs.GetString...,但根据我的经验,它喜欢在最后添加空行/列,然后您必须以某种方式删除,所以除非你要返回数千行和/或数百列,这是不值得的。