当前位置: 首页 > 图文教程 > 网络编程 > ASP > bbs树形结构的实现方法(一)

ASP
ASP编程中15个非常有用的例子 (二)
ASP与JSP的比较(一)
ASP与JSP的比较(二)
ASP中五种连接数据库的方法
从ASP调用SQL中的图像
用排序串字段实现树状结构(例程:连接字串)
用排序串字段实现树状结构(例程:删除贴子)
用排序串字段实现树状结构(例程:回复表单)
用排序串字段实现树状结构(例程:显示贴子内容)
用排序串字段实现树状结构(例程:显示树)
用排序串字段实现树状结构(存储过程)
用排序串字段实现树状结构(库结构)
用排序串字段实现树状结构(原理)
remote script文档(转载自微软)(一)
remote script文档(转载自微软)(二)
remote script文档(转载自微软)(三)
remote script文档(转载自微软)(四)
remote script文档(转载自微软)(五)
remote script文档(转载自微软)(六)
remote script文档(转载自微软)(七)

ASP 中的 bbs树形结构的实现方法(一)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 61 ::
收藏到网摘: n/a

  【bigeagle】 于 2000-12-6 14:38:50 加贴在 Joy ASP ↑:

    bbs的树形结构显示可以有很多种方法,其中比较容易想到的是递归和排序串方法,但这两种方法并不是很好,那么怎
样才算是比较合理的算法呢?
    递归方法不用讲,大家都知道怎么用,先讲讲排序串方法,最简单的排序串方法可以这样用,只用一个id就可以完成树
型,向这样
1  001
2  002
3  001001
4  001001001
5  001002001
用这个字符串排序后就变成这样:
001
   001001
         001001001
   001002001
002
这种方法容易实现,但缺点也是很明显,一个是回帖数受限制,另一个随着回帖增加会越来越长,影响数据库效率。  

下面一种方法是李龙的,属于变通的排序串方法
DDL
--------------
CREATE TABLE dbo.Message
(
    ID          numeric(18,0) IDENTITY(1000,1),
    DateAndTime datetime      DEFAULT getdate() NOT NULL,
    AuthorID    numeric(18,0) NOT NULL,
    Subject     nvarchar(250) NOT NULL,
    Body        ntext         NULL,
    LinkURL     nvarchar(100) NULL,
    TextForLink nvarchar(50)  NULL,
    ImageURL    nvarchar(100) NULL,
    Class       int           DEFAULT 0 NOT NULL,
    ClientInfo  nvarchar(250) NULL,
    RemoteAddr  nvarchar(50)  NULL,
    CONSTRAINT PK_BBSMessage
    PRIMARY KEY NONCLUSTERED (ID,AuthorID)
)
go
CREATE TABLE dbo.MsgRefTab
(
    MsgID      numeric(18,0) NOT NULL,
    ParentID   numeric(18,0) NOT NULL,
    AncestorID numeric(18,0) NOT NULL,
    ChildNum   numeric(18,0) DEFAULT 0 NOT NULL,
    LinkStr    nvarchar(250) NOT NULL,
    CONSTRAINT PK_BBSRefTab
    PRIMARY KEY NONCLUSTERED (MsgID)
)
go
-----------------
存储过程:
-----------------
-- 抽出
CREATE PROCEDURE sp_Summary
@HaveBody bit,
@from numeric,
@to numeric
AS
IF (@HaveBody = 1)
select t.ID,t.DateAndTime,m.Nickname as
Author,m.Email,t.Subject,t.Body,t.LinkURL,t.TextForLink,t.ImageURL,s.ChildNu
m,s.ParentID
from Message t
  ,MsgRefTab AS s
  ,(SELECT MsgID FROM MsgRefTab WHERE ParentID = 0) AS f
  ,Members AS m
where t.ID=s.MsgID
  and f.MsgID = s.AncestorID
  and f.MsgID between @from and @to
  and m.MemberID = t.AuthorID
order by s.AncestorID,s.LinkStr
ELSE
select t.ID,t.DateAndTime,m.Nickname as
Author,m.Email,t.Subject,t.LinkURL,t.TextForLink,t.ImageURL,s.ChildNum,s.Par
entID
from Message t
  ,MsgRefTab AS s
  ,(SELECT MsgID FROM MsgRefTab WHERE ParentID = 0) AS f
  ,Members AS m
where t.ID=s.MsgID
  and f.MsgID = s.AncestorID
  and f.MsgID between @from and @to
  and m.MemberID = t.AuthorID
order by s.AncestorID,s.LinkStr
go

-- 加贴

CREATE PROCEDURE sp_Add_Message
@AuthorID numeric,
@Subject nvarchar(250),
@Body  ntext,
@LinkURL nvarchar(100),
@TextForLink nvarchar(50),
@ImageURL nvarchar(100),
@ParentID numeric,
@ID  numeric OUTPUT,
@ChildNum numeric