非递归生成的TreeView

开始还想去看那个递归的算法呢  基本没看懂...不知道怎么结束递归的...自己想也没想通..
然后看看实际问题..反正我们是个小树,只有右孩子 于是就用非递归了...
经过这次折腾后 对SqlDataAdapter和SqlCommand有了更深的了解


先说下TreeView绑定的数据库的结构
ZD_权限(权限名,权限编号,上级权限,权限URL)
权限分配(用户名,权限编号)          类似学生-选课结构
-
-
生成TreeView代码
-----------------------------------------------------------------------------------------------
    protected void BindDataBase()
    {
        string username = Request.Cookies["username"].Value;
        //string username = "admin";
        SqlConnection conn =new SqlConnection(ConfigurationManager.AppSettings["Myconn"]);
       // clsDB dbo = new clsDB();
       // SqlConnection conn1 =new SqlConnection(dbo.connectionString());
        string sqlstr1 = "SELECT ZD_权限.权限名,ZD_权限.上级权限,ZD_权限.权限URL,权限分配表.权限编号 FROM ZD_权限 INNER JOIN 权限分配表 ON ZD_权限.权限编号 = 权限分配表.权限编号 WHERE (权限分配表.用户名 ='"+ username +"' and 上级权限='000')";
        SqlDataAdapter da1 = new SqlDataAdapter(sqlstr1,conn);
        DataSet ds1 = new DataSet();
        da1.Fill(ds1,"allfather");
        for(int i=0;i        {   //父节点的创建
            TreeNode tree2 = new TreeNode();
            tree2.Text = ds1.Tables["allfather"].Rows["权限名"].ToString();
            tree2.Target = "right";  //作用于右框架
            tree2.SelectAction = TreeNodeSelectAction.Expand;  //父节点选中展开
            TreeView1.Nodes.Add(tree2);
            
            //子节点创建
            string sqlstr2 = "SELECT ZD_权限.权限名,ZD_权限.上级权限,ZD_权限.权限URL,权限分配表.权限编号 FROM ZD_权限 INNER JOIN 权限分配表 ON ZD_权限.权限编号 = 权限分配表.权限编号 WHERE (权限分配表.用户名 ='"+ username +"' and 上级权限='"+ ds1.Tables["allfather"].Rows["权限编号"].ToString()+"')";
            da1 = new SqlDataAdapter(sqlstr2, conn);
            DataSet ds2 = new DataSet();
            da1.Fill(ds2, "allchild");
            for (int j = 0; j < ds2.Tables["allchild"].Rows.Count; j++)
            {  
                TreeNode tree3 = new TreeNode();
                tree3.Text=ds2.Tables["allchild"].Rows[j]["权限名"].ToString();
                tree3.NavigateUrl = ds2.Tables["allchild"].Rows[j]["权限URL"].ToString();
                tree3.Target = "right";   //作用于右框架
                tree2.ChildNodes.Add(tree3);
            }
        }
    }

转载于:https://www.cnblogs.com/wpf123/archive/2009/04/21/2347498.html

你可能感兴趣的:(非递归生成的TreeView)