表结构模型:t1 
nodeid parentnodeid
所有节点信息都存在一个表里,节点信息随时可能被增删改。除了在前台递归进行多次查询以外有什么好的办法绑定树结构么?现在有一个不成熟的想法,就是在数据库里递归,把所有节点信息一次全查出来到前台处理,减少查询次数。不过不会写递归的sql

解决方案 »

  1.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,name varchar(50),parentid int)
    insert into tb select 1,'安徽省',0
    insert into tb select 2,'湖南省',0
    insert into tb select 3,'合肥市',1
    insert into tb select 4,'巢湖市',1
    insert into tb select 5,'肥西县',3
    insert into tb select 6,'肥东县',3
    insert into tb select 7,'肥东乡',6
    insert into tb select 8,'肥东村',7
    insert into tb select 9,'湖南乡',2
    insert into tb select 10,'湖南镇',9
    gowith dom 
    as
    (select * from tb where parentid=3 
    union all
    select a.* from tb a inner join dom b on a.parentid=b.id)select * from dom
      

  2.   

    即使你在后臺寫了遞歸也沒用的
    因為總是要在前臺表現出來的。不過可以一次性把前臺需要的數據一次性帥選出來,存在一個datatable
    然后再表示層進行遞歸。(這樣就不用多次查詢數據庫了)