求C#中写一个递归删除的方法,这张表的字段是ID,NAME,PID, PID存的是上级的ID。这个方法参数只有ID和实体Entity。
实体是数据库中映射出来的!
实体是数据库中映射出来的!
解决方案 »
- 请教一个c#中picturebox控件的问题
- VS2008可否生成免安装的exe文件
- 字符串问题
- 需要通过互联网向服务器发送大量图片,请问采用什么方式做比较好
- html空间
- 请教一个运算符优先级的问题,谢谢!
- 安装Windows service出现问题
- 我快崩溃啦, MSDN装了后没法用
- using System.Management;编译时提示“命名空间“Management”在“System”中不存在”?
- How to delete rows in datatabel?
- Mapx 自定义selectionTool,问题提问!
- ClickOnce 不支持请求执行级别requireAdministrator
void delEntityAndChild(int id){
int pid=0;
// 这里查一下 pid = id 的列表,
// select id from "tablename" where pid=@id foreach(datarow dr in dt.rows)
{
// 这里递归了, 查得到的 ID 有没有 pid 等于它的
int tmppid = Convert.ToInt32(dr["id"]);
// 大于 0 是考虑,可能一级节点的 pid =0
if(tmppid >0){
delEntityAndChild(pid);
}
// 执行删除
// delete from "tablename" where id = @id
}
}
use DBTest
go
if OBJECT_ID('tabTest') is not null drop table tabTest
go
create table tabTest
(
ID int,
Name nvarchar(20),
PID int
)
go
insert into tabTest
select 1,'a',0 union all
select 2,'b',1 union all
select 3,'c',0create proc procTest(@ID int)
AS
with CTETest
as
(
select * from tabTest where ID=@ID
union all
(
select a.* from tabTest as a inner join
CTETest as b on a.PID=b.ID
)
)
delete from tabTest where ID in(select ID from CTETest)
GO