0

我在 VBscript 中有一个 ADO 查询(ADODB.Recordset),它将返回如下结果:

nId wstrName              nParentId
0   Managed computers     2
1   Unassigned computers  NULL
2   Master Server         NULL
3   pseudohosts           NULL
5   Server 2              0
8   Group100              5
10  Group22               5
11  Group47               5
13  Group33               5
14  Group39               5
15  Group11               5

当我知道顶级组 ID 时,我需要根据结果构建一个完整的位置字符串。

例如,如果顶级组 ID 为 11,则在通过查看“nId”和“nParentId”值遍历组后,完整的位置字符串将是“主服务器/托管计算机/服务器 2/Group47”。

父组的数量可能会有所不同,因此我需要循环直到到达没有父组的组。我还想避免进行多个 SQL 查询,所以我假设结果应该被加载到一个数组中,然后从那里处理信息。

处理这个问题的最佳方法是什么?

提前致谢 :)

4

2 回答 2

1

您可以按原样使用记录集。尝试这个:

groupname = "..."

rs.MoveFirst
rs.Find("wstrName = '" & groupname & "'")
location = rs("wstrName")
parent   = rs("nParentId")
Do Until parent = "NULL"
  rs.MoveFirst
  rs.Find("nId = " & parent)
  location = rs("wstrName") & "/" & location
  parent   = rs("nParentId")
Loop

您可能需要根据NULL记录集中的值是字符串"NULL"还是实际Null值来调整循环的条件。

于 2013-03-19T23:24:14.600 回答
0

使用字典来存储您的表格数据,并使用递归函数来构建位置字符串。

演示脚本:

  Dim dicData : Set dicData = CreateObject("Scripting.Dictionary")
'         nId         wstrName                nParentId
  dicData( 0) = Array("Managed computers"   , 2   )
  dicData( 1) = Array("Unassigned computers", NULL)
  dicData( 2) = Array("Master Server"       , NULL)
  dicData( 3) = Array("pseudohosts"         , NULL)
  dicData( 5) = Array("Server 2"            , 0   )
  dicData( 8) = Array("Group100"            , 5   )
  dicData(10) = Array("Group22"             , 5   )
  dicData(11) = Array("Group47"             , 5   )
  dicData(13) = Array("Group33"             , 5   )
  dicData(14) = Array("Group39"             , 5   )
  dicData(15) = Array("Group11"             , 5   )
  Dim nId
  For Each nId In dicData.Keys()
      WScript.Echo Right(100 + nId, 2), buildLoc(dicData, nId, Array())
  Next

Function buildLoc(dicData, nId, aTmp)
  ReDim Preserve aTmp(UBound(aTmp) + 1)
  aTmp(UBound(aTmp)) = dicData(nId)(0)
  If IsNull(dicData(nId)(1)) Then
     reverseArr aTmp
     buildLoc = Join(aTmp, "/")
  Else
     buildLoc = buildLoc(dicData, dicData(nId)(1), aTmp)
  End If
End Function

Sub reverseArr(aX)
  Dim nUB  : nUB  = UBound(aX)
  Dim nUB2 : nUB2 = nUB \ 2
  Dim i, vt
  For i = 0 To nUB2
      vt          = aX(i)
      aX(i)       = aX(nUB - i)
      aX(nUB - i) = vt
  Next
End Sub

输出:

00 Master Server/Managed computers
01 Unassigned computers
02 Master Server
03 pseudohosts
05 Master Server/Managed computers/Server 2
08 Master Server/Managed computers/Server 2/Group100
10 Master Server/Managed computers/Server 2/Group22
11 Master Server/Managed computers/Server 2/Group47
13 Master Server/Managed computers/Server 2/Group33
14 Master Server/Managed computers/Server 2/Group39
15 Master Server/Managed computers/Server 2/Group11
于 2013-03-19T22:52:50.827 回答