有在表 新闻类别 info_newsSort(sortID,sortName,parentID) 要求为输入一个sortID  得到他的所有子节点
存储过程 info_news_getChildIID    ALTER PROCEDURE [dbo].[info_news_getChildIID]
(
@sortID int
)
AS
BEGIN
select * from info_newsSort where parentID= @sortID 
END
c#代码  public  void news_getCIDBySortID(int sortID,string cIDList)
  {
            SqlParameter[] para = { new SqlParameter("@sortID",SqlDbType.Int)};
            para[0].Value = sortID;
            DataTable dt = SqlHelper.ExecuteDataset("info_news_getChildIID", para).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cIDList += dt.Rows[i]["sortID"].ToString() + ",";//把所有子节点构成一个string 已,隔开
                str1 = cIDList;//str1为全局变量,我在外面已经定以后好
                news_getCIDBySortID(Convert.ToInt32(dt.Rows[i]["sortID"]),cIDList);
               
                
            }
  }现在这方法经测试str1不对 不能取全,请问问题出在哪 或哪位有更好的方法的 谢谢!

解决方案 »

  1.   

    select * from info_newsSort where parentID= @sortID 
    只判断了它的子节点,没判断它的孙子节点等等...
      

  2.   

    肯定是不能啊。你str1递归的时候从新赋值了
      

  3.   

    已经解决了 
    str1 = cIDList;
    去掉
    news_getCIDBySortID(Convert.ToInt32(dt.Rows[i]["sortID"]),cIDList);
                   
    改为
     news_getCIDBySortID(Convert.ToInt32(dt.Rows[i]["sortID"]), str1);
    就行 当初不知道怎么想的 多此一举大家如果有好的方法 介绍下 哎~我太菜了 晕死
      

  4.   


    --生成测试数据
    create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
    insert into BOM select 1,0,NULL
    insert into BOM select 2,1,NULL
    insert into BOM select 3,1,NULL
    insert into BOM select 4,2,NULL
    insert into BOM select 5,3,NULL
    insert into BOM select 6,5,NULL
    insert into BOM select 7,6,NULL
    go
    select * from BOM
    go
    --创建用户定义函数
    create function f_getChild(@ID VARCHAR(10))
    returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
    as
    begin
        declare @i int,@ret varchar(8000)
        set @i = 1
        insert into @t select ID,PID,@i from BOM where PID = @ID
        
        while @@rowcount<>0
        begin
            set @i = @i + 1
            
            insert into @t 
            select 
                a.ID,a.PID,@i 
            from 
                BOM a,@t b 
            where 
                a.PID=b.ID and b.Level = @i-1
        end
        return
    end
    go--执行查询
    select ID from dbo.f_getChild(3)
    go--输出结果
    /*
    5
    6
    7
    */--删除测试数据
    drop function f_getChild
    drop table BOM