获取所有子节点 父亲节点

 

//子节点

  Expo.Database.SqlGen mycmd = new Expo.Database.SqlGen();
    protected void Page_Load(object sender, EventArgs e)
    {
        IList<string> path = new List<string>();
        GetPath(2, ref path);

        foreach (string s in path) {
            Response.Write(s + "<br/>");
        }
    }


    public void GetPath(int father,ref IList<string> path)
    {
        string strsql = "select id,title,father from t_Area where father=" + father.ToString();
        using (SqlDataReader dr1 = mycmd.ExecuteReader(strsql))
        {
            if (!dr1.HasRows)
                return;
            while (dr1.Read())
            {
                path.Add(dr1["title"].ToString());
                GetPath(Convert.ToInt32(dr1["id"]),ref path);
            }
            dr1.Close();
            dr1.Dispose();
        }
    }

 

 

//父亲节点

 string strsql = @" declare @tempTable TABLE
                            (
                             [id] int,
                             Name varchar(50),
                             Depth int,
                             ParentID int,
                             Rank int
                            )
                            declare @ID int
                            set @ID={0}
                            while(@ID>0)
                            begin
                                select @ID=ParentID from t_Category where ID=@ID;
                                if(@ID>0)
                                begin
                                insert into @tempTable select * from t_Category where ID=@ID;
                                end
                            end
                            select * from @tempTable order by id ";

posted @ 2009-05-13 11:08  awp110  阅读(367)  评论(0编辑  收藏  举报