用递归读取数据库(*.MDB)生成树节点(TreeNode)

加载数据库记录生成树节点(TreeNode),才1500多个记录就要用4分多钟,实在太慢,经过思考,减少了一些不必要的查询和赋值,速度提升得比较明显,可能是机器较旧,最终结果都不理想,当然,这个与自己的技术太烂有关。


数据库中有1500多个记录,用递归的方法将记录一次性全部添加到TreeNode当中,第一次加载用时4分多钟,修改之后3分多钟,再次修改后是1分多钟。项目运行环境:单核CPU,2GB DDR2内存,Win7 x86旗舰版,C# 2010 Express,.NET4,ACCESS(*.MDB)数据库。

原代码:
        //递归添加子节点
        public static void AddTreeNode(TreeNode node,String GUID)
        {
            node.Name = GUID;
            DataTable table = PublicTools.AccessHelper.ExecuteDataTable("select * from FamilyTree where P_GUID = @P_GUID AND Generation > 1", new OleDbParameter("@P_GUID", node.Name));
            foreach (DataRow row in table.Rows)
            {
                TreeNode n = new TreeNode();
                n.Name = row["M_GUID"].ToString();
                n.Text = row["NameText"].ToString();
                n.Tag = row["Code"].ToString();
                node.Nodes.Add(n);
                AddTreeNode(n,n.Name);
            }
        }

第一次修改:减少了一个多余的参数,注释掉TreeNode的Tag属性赋值
         //递归添加子节点
        public static void AddTreeNode(TreeNode node)
        {
            //node.Name = GUID;
            DataTable table = PublicTools.AccessHelper.ExecuteDataTable("select * from FamilyTree where P_GUID = @P_GUID AND Generation > 1", new OleDbParameter("@P_GUID", node.Name));
            foreach (DataRow row in table.Rows)
            {
                TreeNode n = new TreeNode();
                n.Name = row["M_GUID"].ToString();
                n.Text = row["NameText"].ToString();
                //n.Tag = row["Code"].ToString();
                node.Nodes.Add(n);
                AddTreeNode(n);
            }
        }


第二次修改:将查询语句返回的列由所有(*)改为要用到两列(M_GUID,NameText)
        //递归添加子节点
        public static void AddTreeNode(TreeNode node)
        {
            //node.Name = GUID;
            DataTable table = PublicTools.AccessHelper.ExecuteDataTable("select M_GUID,NameText from FamilyTree where P_GUID = @P_GUID AND Generation > 1", new OleDbParameter("@P_GUID", node.Name));
            foreach (DataRow row in table.Rows)
            {
                TreeNode n = new TreeNode();
                n.Name = row["M_GUID"].ToString();
                n.Text = row["NameText"].ToString();
                //n.Tag = row["Code"].ToString();
                node.Nodes.Add(n);
                AddTreeNode(n);
            }
        }

你可能感兴趣的:(递归,treenode)