我想使用 C# 从源文件夹复制文件夹结构并将其粘贴到同一个表中的目标文件夹中。
以下是WorkingDir表结构:
| Id | ParentId | Name |
| 1 | null | RootFolder |
| 2 | 1 | SubFolder1 |
| 3 | 1 | SubFolder2 |
| 4 | 2 | SubSubFolder1 |
| 5 | null | RootFolder2 |
复制 RootFolder1 并将其粘贴到 RootFolder2 后的WorkingDir 表结构:
| Id | ParentId | Name |
| 1 | null | RootFolder1 |
| 2 | 1 | SubFolder1 |
| 3 | 1 | SubFolder2 |
| 4 | 2 | SubSubFolder1 |
| 5 | null | RootFolder2 |
| 6 | 5 | RootFolder1 |
| 7 | 6 | SubFolder1 |
| 8 | 6 | SubFolder2 |
| 9 | 7 | SubSubFolder1 |
我只有一个数据库,并且只有一张名为WorkingDir的表。
下面的参数是从UI端或者swagger传递过来的。
[
{
"copySourceFolderId": 1,
"pasteDestinationFolderId": 5
}
]
第一步:根据copySourceFolderId从表中获取第N层树形数据结构。并将结果存储到c#中的变量中。
Qyery:从下面的查询中获取步骤 1 结果。
with recursive subordinate as
(
select "Id" ,"Name" ,"ParentId" from ol."WorkingDirectory" w where "Id" = 1
union
select wd2."Id" ,wd2."Name" ,wd2."ParentId" from ol."WorkingDirectory" wd2
inner join subordinate s on s."Id"= wd2."ParentId"
)
select s."Id",s."Name",s."ParentId"
from subordinate s
group by s."Id",s."Name",s."ParentId"
order by s."Id" asc
第2步:从第1步的结果需要在目标下一一添加或插入所有文件夹数据结构。
据我了解,你需要亲子关系,
请检查我在 SQL 中准备的这个查询。
DECLARE @WorkingDirectory TABLE
(
Id int IDENTITY(1,1),
Name NVARCHAR(MAX),
ParentId INT NULL
)
INSERT INTO @WorkingDirectory
VALUES('RootFolder',NULL),
('SubFolder1',1),
('SubFolder2 ',1),
('SubSubFolder1 ',2),
('RootFolder2',NULL),
('RootFolder1 ',5),
('SubFolder1 ',6),
('SubFolder2 ',6),
('SubSubFolder1',7)
;with subordinate as
(
select Id ,Name ,ParentId from @WorkingDirectory w where ParentId IS NULL
union all
select wd2.Id ,wd2.Name ,wd2.ParentId from @WorkingDirectory wd2
inner join subordinate s on s.Id= wd2.ParentId
)
select s.Id,s.Name,s.ParentId
from subordinate s
order by s.Id asc
OutPut