asp读取数据库关联的2个表之间的数据并以JSON格式输出

ASP   2025-02-27 17:50   45   0  

以数组格式输出:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Response.ContentType = "text/html; charset=utf-8"%>
<%
'豆包
' 数据库连接信息
Dim conn, connStr
Set conn = Server.CreateObject("ADODB.Connection")
connStr = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=db;User ID=sa;Password=123456;"
conn.Open connStr

' 父类SQL语句
Dim parentSQL, parentRS
parentSQL = "select id,MingCheng from BDClass where ParentID='1' order by orders"
'conn.CursorLocation = adUseClient
'Set parentRS = conn.Execute(parentSQL)
'parentRS.cursorlocation=3
Set parentRS = Server.CreateObject("ADODB.Recordset")
parentRS.Open parentSQL,conn,3,2

' 定义父类和子类数组
Dim parentArray, childArray
Dim parentIndex, childIndex
parentIndex = 0
ReDim parentArray(parentRS.RecordCount - 1, 1)

' 循环父类数据
Do While Not parentRS.EOF
    ' 保存父类数据到数组
    parentArray(parentIndex, 0) = parentRS("id")
    parentArray(parentIndex, 1) = parentRS("MingCheng")
    ' 子类SQL语句
    Dim childSQL, childRS
    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='" & parentRS("id") & "' order by Orders ,T.AddTime desc,T.ID desc"
    'Set childRS = conn.Execute(childSQL)
	Set childRS = Server.CreateObject("ADODB.Recordset")
	childRS.Open childSQL,conn,3,2
    ' 初始化子类数组
    childIndex = 0
    ReDim childArray(childRS.RecordCount - 1, 5)
    ' 循环子类数据
    Do While Not childRS.EOF
        ' 保存子类数据到数组
        childArray(childIndex, 0) = childRS("Comid")
        childArray(childIndex, 1) = childRS("Title")
        childArray(childIndex, 2) = childRS("OutUrl")
        childArray(childIndex, 3) = childRS("TJtitle")
        childArray(childIndex, 4) = childRS("Logo")
        childArray(childIndex, 5) = childRS("ZSstar")
        childIndex = childIndex + 1
        childRS.MoveNext
    Loop
    ' 关闭子类记录集
    childRS.Close
    Set childRS = Nothing
    ' 可以在这里使用子类数组进行其他操作,例如输出或处理
    parentIndex = parentIndex + 1
    parentRS.MoveNext
Loop
' 关闭父类记录集和数据库连接
parentRS.Close
Set parentRS = Nothing
conn.Close
Set conn = Nothing

' 可以在这里使用父类数组进行其他操作,例如输出或处理
Response.Write "<h2>所有父类信息:</h2>"
Response.Write "<ul>"
For i = LBound(parentArray, 1) To UBound(parentArray, 1)
    Response.Write "<li>父类ID: " & parentArray(i, 0) & ", 父类名称: " & parentArray(i, 1) & "</li>"
Next
Response.Write "</ul>"

Response.Write "<ul>"
For i = LBound(childArray, 1) To UBound(childArray, 1)
    Response.Write "<li>子类ID: " & childArray(i, 0) & ", 子类名称: " & childArray(i, 1) & "</li>"
Next
Response.Write "</ul>"
%>


以JSON格式输出:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Response.ContentType = "text/html; charset=utf-8"%>
<%
'豆包,基于ceshi1输出JSON格式
' 数据库连接信息
Dim conn, connStr
connStr = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=db;User ID=sa;Password=123456;"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connStr

' 父类SQL语句
Dim parentSQL, parentRS
parentSQL = "select id,MingCheng from BDClass where ParentID='1' order by orders"
Set parentRS = conn.Execute(parentSQL)

Dim parentData
parentData = "["
Dim firstParentRecord
firstParentRecord = true

' 循环父类数据
Do While Not parentRS.EOF
    If Not firstParentRecord Then
        parentData = parentData & ","
    End If
    
    Dim parentId
    parentId = parentRS("id")
    Dim parentName
    parentName = parentRS("MingCheng")
    
    ' 子类SQL语句
    Dim childSQL, childRS
    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)
    
    Dim childData
    childData = "["
    Dim firstChildRecord
    firstChildRecord = true
    
    ' 循环子类数据
    Do While Not childRS.EOF
        If Not firstChildRecord Then
            childData = childData & ","
        End If
        
        childData = childData & "{"
        childData = childData & """Comid"":""" & childRS("Comid") & ""","
        childData = childData & """Title"":""" & childRS("Title") & ""","
        childData = childData & """OutUrl"":""" & childRS("OutUrl") & ""","
        childData = childData & """TJtitle"":""" & childRS("TJtitle") & ""","
        childData = childData & """Logo"":""" & childRS("Logo") & ""","
        childData = childData & """ZSstar"":""" & childRS("ZSstar") & """"
        childData = childData & "}"
        
        firstChildRecord = false
        childRS.MoveNext
    Loop
    
    childData = childData & "]"
    
    ' 关闭子类记录集
    childRS.Close
    Set childRS = Nothing
    
    parentData = parentData & "{"
    parentData = parentData & """id"":""" & parentId & ""","
    parentData = parentData & """MingCheng"":""" & parentName & ""","
    parentData = parentData & """children"":" & childData
    parentData = parentData & "}"
    
    firstParentRecord = false
    parentRS.MoveNext
Loop

parentData = parentData & "]"

' 关闭父类记录集和数据库连接
parentRS.Close
Set parentRS = Nothing
conn.Close
Set conn = Nothing

' 设置响应头为 JSON 格式
Response.ContentType = "application/json"
Response.Charset = "UTF-8"
Response.Write parentData
%>




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