SQL Server存储的层级数据读出成树显示


  • 来源:

2008-11-18 11:18:26 北京时间

前两天我们介绍了一种新的存储无限级分类方法,但是读出分类的时候怎么实现树形显示呢?方法很简单,我们自己定义一个树形的数据结构,然后根据数据库存储的节点的深度来插入到树里面,当然显示的时候需要用递归来显示一下,不过这里的递归只是在内存里面递归,效率是相当高的。

 

在数据库读出数据的时候直接按照 LID 来进行 ASC 排序就可以了,默认的排列顺序就是按照树走的,大家可以插入一些数据,并读取一下就可以很明了的看到顺序了,插入树循环的时候只需要对深度进行运算就可以了。

下面我只写出了一些关键地方的代码,具体的代码自己试着多写写在纸上多画画应该就明白了。

另外就是想说下,这种分类算法只适用于一般的树形分类,并不适用于插入数据比较频繁的树形结构,比如说无限次回复的评论,无限次回复的评论有另外一种更适合的算法。

首先我们定义一个树形的数据结构:

public class ZoneList  

{  

    private readonly ZoneList _Parent = null;  

    private readonly List<ZoneList> _Childs = new List<ZoneList>();  

    private readonly ZoneItem _Value = null;  

 

    public ZoneList Parent { get { return _Parent; } }  

 

    public List<ZoneList> Childs { get { return _Childs; } }  

 

    public ZoneItem Value { get { return _Value; } }  

 

    public ZoneList Root  

    {  

        get 

        {  

            ZoneList curNode = this;  

            while (curNode.Parent != null)  

            {  

                curNode = curNode.Parent;  

            }  

            return curNode;  

        }  

    }  

 

    public ZoneList() { }  

 

    public ZoneList(ZoneItem value, ZoneList parent)  

    {  

        _Value = value;  

        _Parent = parent;  

    }  

 

    public ZoneList AddChild(ZoneItem value)  

    {  

        ZoneList nZoneList = new ZoneList(value, this);  

        _Childs.Add(nZoneList);  

        return nZoneList;  

    }  

然后读取数据库并插入到树:

public ZoneList Select()  

{  

    ZoneList oZoneList = new ZoneList();  

    ZoneItem oZoneItem;  

 

    Int32 intDee = 0;  

 

    SqlDataReader oData = SiteSQL.ExecuteReader("ZoneSelect");  

    while (oData.Read())  

    {  

        oZoneItem = new ZoneItem();  

        oZoneItem.ID = oData.GetInt32(0);  

        oZoneItem.Tree = oData.GetInt32(1);  

        oZoneItem.Name = oData.GetString(2);  

        intDee = intDee - oZoneItem.Tree;  

 

        for (Int32 intI = 0; intI <= intDee; intI++)  

        {  

            oZoneList = oZoneList.Parent;  

        }  

 

        oZoneList = oZoneList.AddChild(oZoneItem);  

 

        intDee = oZoneItem.Tree;  

    }  

    oData.Close();  

    return oZoneList.Root;  

显示的时候直接用递归就可以了:

private void ZoneOutWrite(ZoneList oZoneList)  

{  

    foreach (ZoneList oZoneNode in oZoneList.Childs)  

    {  

        Response.Write(String.Format("<li id=\"zv_{0}\"><span id=\"zName_{0}\">{1}</span>", oZoneNode.Value.ID, oZoneNode.Value.Name));  

 

        if (oZoneNode.Childs.Count > 0)  

        {  

            Response.Write("<ul>");  

            ZoneOutWrite(oZoneNode);  

            Response.Write("</ul>");  

        }  

        Response.Write("</li>");  

    }  




发表评论

请文明参与讨论,禁止漫骂攻击。本站保留不刊登无关和不雅评论的权力。


用户名: 密码: 匿名

相关新闻
1. 确认已经安装了NT/2000和SQL Server的最新补丁程序,不用说大
在SQL Server中进行开发会让你身处险地,并且寻找快速解决方案。
本文说明向外扩展数据库系统的两个选项,从而实现更高的可扩展性
SQL Server 2005的新功能为动态管理对象,它们是在指定时间返回
本文从算法入手同时通过实例来讲解如何在SQL Server数据库中实现

广告
© 2008 The Czz5 Company. All Rights Reserved. 关于我们 - 联系我们 - 广告业务 - 网站地图 - 版权声明 - 友情连接 - 网站投稿 - 致信编辑