34,590
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 删除节点
/// </summary>
/// <param name="node"></param>
private int idelcount = 0;
private void DeleteNodeToDB(TreeNode node)
{
TreeNode currentnode = new TreeNode();
currentnode=node;
if (currentnode != null)
{
string name=currentnode.Name;
SqlConnection con=new SqlConnection(strconn);//你的sql连接语句,这你我不写了
SqlCommand com=new Sqlcommand(con,"delete from A where ID='"+name+"'");
if(con.State == ConnectionState.Close)
con.Open();
idelcount +=com.ExecuteNonQuery()
con.Close();//由于是递归,这里每次都要打开关闭一次。
}
if (currentnode.Nodes.Count > 0)
{
for (int j = 0; j < currentnode.Nodes.Count; j++)
{
DeleteNodeToDB(currentnode.Nodes[j]);
}
}
}
/// <summary>
/// 删除节点
/// </summary>
/// <param name="node"></param>
private int idelcount = 0;
private void DeleteNodeToDB(TreeNode node)
{
TreeNode currentnode = new TreeNode();
currentnode=node;
if (currentnode != null)
{
ZBZone zb = (ZBZone)((ZBZone)currentnode.Tag);
idelcount += DBManagement.Delete(zb.CodeID); //DBManagement.Delete这是删除方法,我封装了,如果你写sql语句,你可以直接在这写,执行就可。
}
if (currentnode.Nodes.Count > 0)
{
for (int j = 0; j < currentnode.Nodes.Count; j++)
{
DeleteNodeToDB(currentnode.Nodes[j]);
}
}
}
create table #temp(id int identity(1,1) primary key,pid int,name varchar(20))
insert into #temp
select 0,'A' union all
select 1,'A1' union all
select 1,'A2' union all
select 2,'A11' union all
select 0,'B'
select * from #temp
declare @id int
set @id = 1
;with cte as(
select * from #temp where id=@id
union all
select t.* from #temp t inner join cte c on c.id=t.pid
)
delete from #temp where id in (select id from cte)
select * from #temp
drop table #temp
/*
删除前
1 0 A
2 1 A1
3 1 A2
4 2 A11
5 0 B
删除后
5 0 B
*/