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


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

 

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

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

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

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

  1. public class ZoneList  
  2. {  
  3.     private readonly ZoneList _Parent = null;  
  4.     private readonly List<ZoneList> _Childs = new List<ZoneList>();  
  5.     private readonly ZoneItem _Value = null;  
  6.  
  7.     public ZoneList Parent { get { return _Parent; } }  
  8.  
  9.     public List<ZoneList> Childs { get { return _Childs; } }  
  10.  
  11.     public ZoneItem Value { get { return _Value; } }  
  12.  
  13.     public ZoneList Root  
  14.     {  
  15.         get 
  16.         {  
  17.             ZoneList curNode = this;  
  18.             while (curNode.Parent != null)  
  19.             {  
  20.                 curNode = curNode.Parent;  
  21.             }  
  22.             return curNode;  
  23.         }  
  24.     }  
  25.  
  26.     public ZoneList() { }  
  27.  
  28.     public ZoneList(ZoneItem value, ZoneList parent)  
  29.     {  
  30.         _Value = value;  
  31.         _Parent = parent;  
  32.     }  
  33.  
  34.     public ZoneList AddChild(ZoneItem value)  
  35.     {  
  36.         ZoneList nZoneList = new ZoneList(value, this);  
  37.         _Childs.Add(nZoneList);  
  38.         return nZoneList;  
  39.     }  

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

  1. public ZoneList Select()  
  2. {  
  3.     ZoneList oZoneList = new ZoneList();  
  4.     ZoneItem oZoneItem;  
  5.  
  6.     Int32 intDee = 0;  
  7.  
  8.     SqlDataReader oData = SiteSQL.ExecuteReader("ZoneSelect");  
  9.     while (oData.Read())  
  10.     {  
  11.         oZoneItem = new ZoneItem();  
  12.         oZoneItem.ID = oData.GetInt32(0);  
  13.         oZoneItem.Tree = oData.GetInt32(1);  
  14.         oZoneItem.Name = oData.GetString(2);  
  15.         intDee = intDee - oZoneItem.Tree;  
  16.  
  17.         for (Int32 intI = 0; intI <= intDee; intI++)  
  18.         {  
  19.             oZoneList = oZoneList.Parent;  
  20.         }  
  21.  
  22.         oZoneList = oZoneList.AddChild(oZoneItem);  
  23.  
  24.         intDee = oZoneItem.Tree;  
  25.     }  
  26.     oData.Close();  
  27.     return oZoneList.Root;  

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

  1. private void ZoneOutWrite(ZoneList oZoneList)  
  2. {  
  3.     foreach (ZoneList oZoneNode in oZoneList.Childs)  
  4.     {  
  5.         Response.Write(String.Format("<li id=\"zv_{0}\"><span id=\"zName_{0}\">{1}</span>", oZoneNode.Value.ID, oZoneNode.Value.Name));  
  6.  
  7.         if (oZoneNode.Childs.Count > 0)  
  8.         {  
  9.             Response.Write("<ul>");  
  10.             ZoneOutWrite(oZoneNode);  
  11.             Response.Write("</ul>");  
  12.         }  
  13.         Response.Write("</li>");  
  14.     }  

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3