treeView的动态加载,动态树treeView

动态生成树treeView

 

根节点:

string ConnectionString = "Data Source=zhpt;user=nwvideo;password=nwvideo;";//写连接串 
OracleConnection conn = new OracleConnection(ConnectionString);//创建一个新连接 

conn.Open();

OracleCommand cmd = conn.CreateCommand();

            cmd.CommandText = "SELECT dname FROM nwvideo.device ";//在这儿写sql语句 
            OracleDataReader drNodes = cmd.ExecuteReader();//创建一个OracleDateReader对象 

            // 针对数据库的字段名称,建立与之适应显示表头

            if (drNodes.HasRows)
            {

                while (drNodes.Read())
                {
                    //MessageBox.Show(dr.GetOracleString(0).ToString());
                    TreeNode chile = new TreeNode(drNodes.GetOracleString(0).ToString());
                    treeView1.Nodes.Add(chile);
                    NodesCunt++;//记录当前根节点,第一个是根结点是0,第2个是1,初始值是-1,第一次执行自加1
                    AddChildNodes(chile, drNodes, NodesCunt);
                    
                }
            }

 子结点:

public void AddChildNodes(TreeNode CurrentNode, OracleDataReader dr, int NodesCunt)
        {

            //MessageBox.Show(CurrentNode.ToString());

            string ConnectionString = "Data Source=zhpt;user=nwvideo;password=nwvideo;";//写连接串 
            OracleConnection conn = new OracleConnection(ConnectionString);//创建一个新连接 

            //MessageBox.Show(CurrentNode.Text);

            conn.Open();
            OracleCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT rname FROM nwvideo.n_road where did ='" + CurrentNode.Text + "' ";//在这儿写sql语句 
            //MessageBox.Show("SELECT rname FROM nwvideo.n_road where did ='" + CurrentNode + "' ");
            OracleDataReader dr2 = cmd.ExecuteReader();//创建一个OracleDateReader对象 

            if (dr2.HasRows)
            {
                while (dr2.Read())
                {
                    //int remark = int.Parse(dr2.(0).Length.ToString());

                    //MessageBox.Show(dr2.GetOracleString(0).ToString());
                    TreeNode chile1 = new TreeNode(dr2.GetOracleString(0).ToString());
                    //treeView1.Nodes.Add(chile1);

                    //MessageBox.Show(remark.ToString());

                    //MessageBox.Show("chile1       =" + chile1.Text);

                    treeView1.Nodes[NodesCunt].Nodes.Add(chile1.Text);//NodesCunt:当前根节点,第一个是根结点是0,第2个是1,当前根结点内所有子结点添加完后返回,再给NodesCunt自加1,也就是往下一个根结点内插入子结点
                    //MessageBox.Show( NodesCunt.ToString());//查看当前跟结点
                }
            }
        }

 

黑色头发  http://heisetoufa.iteye.com

你可能感兴趣的:(sql)