asp读取数据库关联的2个表之间的数据并以数组形式输出(5)

ASP   2025-03-01 07:03   39   0  
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="aspjson1.19.asp" -->
<%
' 初始化结果字典
Dim results
Set results = Server.CreateObject("Scripting.Dictionary")

' 连接到数据库
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=db;User ID=sa;Password=123456;" ' 替换为你的数据库连接字符串

' 父类查询
Dim parentId, parentName, parentRs, parentSql
parentSql = "SELECT id, MingCheng FROM BDClass WHERE ParentID='1' ORDER BY orders"
Set parentRs = conn.Execute(parentSql)

' 循环父类
Do While Not parentRs.EOF
    parentId = parentRs("id")
    parentName = parentRs("MingCheng")
    
    ' 创建一个字典来存储当前父类及其子产品
    Dim parentDict
    Set parentDict = Server.CreateObject("Scripting.Dictionary")
    parentDict.Add "id", parentId
    parentDict.Add "MingCheng", parentName
    parentDict.Add "children", Server.CreateObject("Scripting.Dictionary")
    
    ' 子类查询
    Dim childRs, childSql, childId, childTitle, childOutUrl, childTJtitle, childLogo, childZSstar
    childSql = "SELECT C.Comid, C.Title, C.OutUrl, C.TJtitle, C.Logo, C.ZSstar FROM BDcompany T, Company C WHERE T.Comid=C.Comid AND C.IsPass=1 AND T.classid='" & parentId & "' ORDER BY Orders, T.AddTime DESC, T.ID DESC"
    Set childRs = conn.Execute(childSql)
    
    ' 循环子产品
    Do While Not childRs.EOF
        childId = childRs("Comid")
        childTitle = childRs("Title")
        childOutUrl = childRs("OutUrl")
        childTJtitle = childRs("TJtitle")
        childLogo = childRs("Logo")
        childZSstar = childRs("ZSstar")
        
        ' 创建一个字典来存储当前子产品
        Dim childDict
        Set childDict = Server.CreateObject("Scripting.Dictionary")
        childDict.Add "Comid", childId
        childDict.Add "Title", childTitle
        childDict.Add "OutUrl", childOutUrl
        childDict.Add "TJtitle", childTJtitle
        childDict.Add "Logo", childLogo
        childDict.Add "ZSstar", childZSstar
        
        ' 添加到父类的children字典中
        parentDict("children").Add childId, childDict
        childRs.MoveNext
    Loop
    ' 添加到结果字典中
    results.Add parentId, parentDict
    parentRs.MoveNext
Loop

' 关闭数据库连接
parentRs.Close
Set parentRs = Nothing
conn.Close
Set conn = Nothing

' 输出results字典内容
Dim parentKey
For Each parentKey In results.Keys
    ' 分开声明和赋值
    Dim currentParent
    Set currentParent = results(parentKey)
    Response.Write "父类ID: " & currentParent("id") & "<br>"
    Response.Write "父类名称: " & currentParent("MingCheng") & "<br>"
    Response.Write "子类信息:<br>"
    
    Dim childKey
    Dim childrenDict
    Set childrenDict = currentParent("children")
    For Each childKey In childrenDict.Keys
        Dim currentChild
        Set currentChild = childrenDict(childKey)
        Response.Write "  子类ID: " & currentChild("Comid") & "<br>"
        Response.Write "  子类标题: " & currentChild("Title") & "<br>"
        Response.Write "  子类外部链接: " & currentChild("OutUrl") & "<br>"
        Response.Write "  子类推荐标题: " & currentChild("TJtitle") & "<br>"
        Response.Write "  子类Logo: " & currentChild("Logo") & "<br>"
        Response.Write "  子类星级: " & currentChild("ZSstar") & "<br>"
    Next
    Response.Write "<hr>"
Next
%>


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。