据库:SqlServer2000
表:tree
表结构:
<%@ Language = VBScript%>
<%Option Explicit%>
<%
Dim oConn, sSql
Call DBConnBegin()

Dim sTableName
Dim nCurrent, nLen, aTree()
sTableName = "tree"
nCurrent = 1
nLen = GetRescordCount()

ReDim aTree(nLen)

Call GetPowerName(0)
'Dim i
'For i = 1 To nLen
'Response.Write aTree(i) & "<br>"
'Next
Call DBConnEnd()

Function GetRescordCount()
GetRescordCount = oConn.Execute("select count(id) from " & sTableName)(0)
End Function

Sub GetPowerName(s_Id)
Dim n_ParentId, j, oRs
Set oRs = Server.CreateObject( "ADODB.Recordset" )
sSql = "select id,PowerName,Layer from " & sTableName & " where ParentId=" & s_Id
oRs.Open sSql, oConn, 0, 1
Do While Not oRs.Eof
For j = 1 To oRs("Layer")
response.write " "
Next
aTree(nCurrent) = oRs("PowerName")
response.write aTree(nCurrent) & "<br>"
nCurrent = nCurrent + 1
n_ParentId = oRs("id")
Call GetPowerName(n_ParentId)
oRs.MoveNext
Loop
oRs.Close
Set oRs = Nothing
End Sub

Sub DBConnBegin()
' 如果数据库对象已打开,不要再打开
If IsObject(oConn) = True Then Exit Sub

' 你可以不需要打开数据库连接对象而直接打开记录集对象,但如果你需要打开多个记录集对象的话,效率是很低的。
' 如果你不创建一个数据库连接对象,ADO会在每个记录集打开时自动创建一个新的数据库连接对象,就算你用的是相同的SQL语句。
Set oConn = Server.CreateObject("ADODB.Connection")

On Error Resume Next

'Provider=SQLOLEDB.1;Server=(local);Initial Catalog =cx_soft;Integrated Security=SSPI;
'Provider=SQLOLEDB.1;Server=(local);Initial Catalog =cx_soft;Trusted_Connection=yes;
oConn.Open "Provider=sqloledb.1;Data Source=(local);Initial Catalog=AspNetTest;User Id=sa;Password=;"
If Err.Number > 0 Then
' 完全地退出正在运行的脚本
Response.End
End If

' 创建一个记录集
End Sub

Sub DBConnEnd()
On Error Resume Next
oRs.Close
Set oRs = Nothing
oConn.Close
Set oConn = Nothing
End Sub
%>
相关文章: