{
TreeNode Node = new TreeNode();
if (pNode == null)
{ //添加根节点
Node.Text = Row["Name"].ToString();
treeView1.Nodes.Add(Node);
AddTree(Row["Num"].ToString(), Node); //再次递归
}
else
{ //添加当前节点的子节点
Node.Text = Row["Name"].ToString();
pNode.Nodes.Add(Node);
AddTree(Row["Num"].ToString(), Node); //再次递归
}
}
}
DataSet ds = new DataSet();
private void Form2_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select * from test",conn);
da.Fill(ds);
TreeNode pNode=null;
AddTree("0", pNode);
}
下面是B/S结构的代码:
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select * from test", conn);
da.Fill(ds);
TreeNode pNode = null;
AddTree("0", pNode);
}
}
public void AddTree(string fatherNum, TreeNode pNode)
{
DataRow[] dr = ds.Tables[0].Select("fatherNum=" + fatherNum);
if (dr.Length > 0)
{
foreach (DataRow d in dr)
{
TreeNode tNode = new TreeNode();
tNode.Text = d["Name"].ToString();
if (pNode == null)
{
//添加根节点
TreeView1.Nodes.Add(tNode);
}
else
{
//添加当前节点的子节点
pNode.ChildNodes.Add(tNode);
tNode.Collapse();
}
AddTree(d["Num"].ToString(), tNode); //递归
}
}
}
=================================================================================
SQL的递归查询
create procedure GetData(@a nvarchar(50))
as
begin
create table #Temp(
a nvarchar(50),
b nvarchar(50),
c nvarchar(50)
)
declare @tempB nvarchar(50)
insert into #Temp select a,b,c from T where a in(@a)
declare cur cursor for select b from #Temp
open cur
fetch next from cur into @tempB
while @@fetch_status = 0
begin
insert into #Temp select a,b,c from T where a in(@tempB)
fetch next from cur into @tempB
end
close cur
deallocate cur
select * from #Temp
end
=====================================================
上面的是假设表结构如下的:
a b c
=========================
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34912-3.html
刚买不久的6S