无须迭代两个循环完成树结构构建

 

该算法思想是

        第一步循环完成TreeNode构建

        第二次循环完成层次结构构建

        数据存储采用一般层次结构存储方式  一次调用数据库完成所有所需数据  采用数组索引存储树节点与ParentSQN避免了查找性能上的缺点

表格:

None.gif DepartmentID  uniqueidentifier
None.gifDepartmentSQN 
int
None.gifDepartmentName 
nvarchar ( 32 ), 
None.gifParentID 
uniqueidentifier  None.gif

存储过程:

None.gif CREATE   PROCEDURE  dbo.Department_SelectCore 
None.gif
AS
None.gif
SET  NOCOUNT  ON  
None.gif
SELECT  DepartmentID, DepartmentSQN, DepartmentName, ParentID ,
None.gif        (
SELECT  DepartmentSQN  FROM  Department Parent  WHERE  (Parent.DepartmentID  =  Department.ParentID))  AS  
None.gif    ParentSQN 
None.gif
FROM  Department 
None.gif
ORDER   BY  DepartmentSQN
None.gif
RETURN  

数据调用代码:

None.gif [DataObject( true )]
None.gif
public   class  DepartmentDataObject
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  [DataObjectMethod(DataObjectMethodType.Select, 
true)]
InBlock.gif  
public static DataSet ReadCore()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    Database db 
= DatabaseFactory.CreateDatabase("WorkItemTrack");
InBlock.gif    
string sqlCommand = "Department_SelectCore"
InBlock.gif    DbCommand dbCommand 
= db.GetStoredProcCommand(sqlCommand);
InBlock.gif    db.AddReturnValueParameter(dbCommand);
InBlock.gif    
return db.ExecuteDataSet(dbCommand);
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

数结构构建代码:

 

None.gif protected   void  BuildTree()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif  _DepartmentTreeView.Nodes.Clear();
InBlock.gif  DataSet ds 
= DepartmentDataObject.ReadCore(); 
InBlock.gif  
int Count = ds.Tables[0].Rows.Count;
InBlock.gif  TreeNode[] Nodes 
= new TreeNode[Count + 1];
InBlock.gif  Nullable
<int>[] ParentSQNs = new Nullable<int>[Count + 1];
InBlock.gif  
foreach (DataRow dr in ds.Tables[0].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif    Nodes[((
int)dr["DepartmentSQN"])] = new TreeNode((string)dr["DepartmentName"], ((Guid)dr["DepartmentID"]).ToString());
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      ParentSQNs[(
int)dr["DepartmentSQN"]] = (int)dr["ParentSQN"];
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      ParentSQNs[(
int)dr["DepartmentSQN"]] = null;
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif  }

InBlock.gif  
for (int i = 1; i < Count + 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
if (ParentSQNs[i].HasValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Nodes[ParentSQNs[i].Value].ChildNodes.Add(Nodes[i]);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      _DepartmentTreeView.Nodes.Add(Nodes[i]);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Bolik/archive/2006/09/09/499555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值