生成树的递归实现

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

当然先要有第三方的TREEVIEW控件:)

using Microsoft.Web .UI .WebControls ;

private void InitTree(TreeNodeCollection nds,string mailfrom,long time)

{

//创建一个数据试图

DataView dv=new DataView ();

//创建一个新节点

TreeNode tempnd=new TreeNode ();

//给数据试图添加值

dv.Table =dataset.Tables ["tree"];

//试图过滤

//dv.RowFilter ="MailFrom='" +mailfrom+ "'";

al.Add (mailfrom);

//关闭dataset

dataset.Clear ();

dataset.Dispose ();

myconnection.Close ();

myconnection.Dispose ();

foreach(DataRowView drv in dv)

{

//从试图中获取节点信息

tempnd=new TreeNode ();

//获取节点的ID

tempnd.ID =drv["MailSrc"].ToString ();

//获取节点的TEXT

tempnd.Text =drv["MailSrc"].ToString ();

//获取捕捉时间

string tempnddt=drv["capturetime"].ToString() ;

//将时间转换为LONG便于比较

long temp=long.Parse (tempnddt);

//判定该节点是否可以加入(是否已经存在该节点或者捕捉时间小于父节点的捕捉时间

if(al.Contains (tempnd.Text ) || time>temp)

continue;

else

{

//将该节点加入已存在节点处

al.Add (tempnd.Text );

//树中加入节点

nds.Add (tempnd);

//递归

//设置新的数据集

string tempndtxt=tempnd.Text ;

myconnection=new System.Data.SqlClient.SqlConnection ("server=localhost;uid=sa;pwd=sa;database=zhltest");

myconnection.Open ();

mycommand=new System.Data.SqlClient.SqlCommand ();

mycommand.Connection =myconnection;

mycommand.CommandText ="select * from testmail where MailFrom='" +tempndtxt+ "'";

adapter=new System.Data.SqlClient.SqlDataAdapter (mycommand.CommandText ,myconnection);

dataset=new DataSet ();

//将满足条件的存入dataset中

adapter.Fill (dataset,"tree");

InitTree(tempnd.Nodes,tempnd.ID,temp);

}

}

private void Button1_Click(object sender, System.EventArgs e)

{

string query=treetxt.Text ;

myconnection=new System.Data.SqlClient.SqlConnection ("server=localhost;uid=sa;pwd=sa;database=zhltest");

myconnection.Open ();

mycommand=new System.Data.SqlClient.SqlCommand ();

mycommand.Connection =myconnection;

mycommand.CommandText ="select * from testmail where MailFrom='" +query+ "'";

adapter=new System.Data.SqlClient.SqlDataAdapter (mycommand.CommandText ,myconnection);

dataset=new DataSet ();

//将满足条件的存入dataset中

adapter.Fill (dataset,"tree");

InitTree(TreeView1.Nodes ,query,0);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航