数据库Access,字段:ClassID(主键),ParentClassID,ClassName,3个字段都是文本型。
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP无限分类数据库版</title>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<metaname="Generator"content="EditPlus">
<metaname="Author"content="Dicky;QQ:25941">
<metaname="Keywords"content="Dicky;QQ:25941;ASP无限分类数据库版">
<metaname="Description"content="Dicky;QQ:25941;ASP无限分类数据库版">
</head>

<body>
<%
ConstIsSql=0'定义数据库类型,1为SQLServer,0为Access
FunctionOpenConn(Conn)'打开数据库连接
DimConnStr
IfIsSql=1Then'如果是SQLServer数据库
'SQLServer数据库连接参数:用户名、用户密码、数据库名、连接名(本地用local,外地用IP)
DimSqlUsername,SqlPassword,SqlDatabaseName,SqlLocalName
SqlUsername="sa"
SqlPassword=""
SqlDatabaseName="TreeDb"
SqlLocalName="(local)"
ConnStr="Provider=Sqloledb;UserID="&SqlUsername&";Password="&SqlPassword&";InitialCatalog="&SqlDatabaseName&";DataSource="&SqlLocalName&";"
Else'如果是Access数据库
DimDb
'第一次使用请修改本处数据库地址并相应修改数据库名称,如将Dicky.mdb修改为Dicky.asp(防止恶意下载Access数据库)
Db="TreeDB.mdb"
ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&Server.MapPath(Db)
EndIf
OnErrorResumeNext
SetConn=Server.CreateObject("ADODB.Connection")
Conn.OpenConnStr
IfErrThen
'Err.Clear
SetConn=Nothing
Response.Write"数据库连接出错,请检查连接字串。"
Response.End
EndIf
EndFunction

FunctionCloseConn(Conn)'关闭数据库连接
IfIsObject(Conn)Then
Conn.Close
SetConn=Nothing
EndIf
EndFunction

FunctionEcho(Str)'输出字符串并换行
Response.WriteStr&VbCrlf
EndFunction

CallOpenConn(Conn)

'定义第一级分类
SubMainFl()
DimRs
SetRs=Conn.Execute("SELECTClassID,ClassNameFROMClassWHEREParentClassIDISNULL")
IfNotRs.EofThen
DoWhileNotRs.Eof
Echo("<div><labelid="""&Trim(Rs("ClassID"))&""">+"&Trim(Rs("ClassName"))&"</label>")
CallSubfl(Rs("ClassID"),"|-")'循环子级分类
Echo("</div>")
Rs.MoveNext
IfRs.EofThenExitDo'防上造成死循环
Loop
EndIf
SetRs=Nothing
EndSub
'定义子级分类
SubSubFl(FID,StrDis)
DimRs1
SetRs1=Conn.Execute("SELECTClassID,ClassNameFROMClassWHEREParentClassID='"&FID&"'")
IfNotRs1.EofThen
DoWhileNotRs1.Eof
Echo("<divid="""&Trim(Rs1("ClassID"))&""">"&StrDis&Trim(Rs1("ClassName"))&"</div>")
CallSubFl(Trim(Rs1("ClassID")),"|"&Strdis)'递归子级分类
Rs1.Movenext:Loop
IfRs1.EofThen
Rs1.Close
ExitSub
EndIf
EndIf
SetRs1=Nothing
EndSub

'最后直接调用MainFl()就行了

MainFl()

CallCloseConn(Conn)%>
</body>
</html>




























































































