不是数据量大的问题,而是你跑了太多个query。比方说,即使第一个select返回了1000条记录并且每个记录都没有子节点,你也要往数据库跑1001个来回,能不慢吗?简单的解决办法是写一个SQL Stored Proc,把这个循环逻辑放在里面,然后在C#里面只要调用这个Stored Proc,一次就能返回整个要求的树结构了。这里有一个Stored Proc读树结构的例子(level相关的部分可以省掉):
http://www.vbforums.com/showthread.php?t=366078

解决方案 »

  1.   

    最基本的方法 查询语句 select * 改掉~~用到哪个字段查哪个字段,不要 “*”
      

  2.   

    我说下我的方法:
    1.数据库的读取,还是建议一次性读取完比较好, 如果每次只读取一部分的话 后面很有可能会频繁连接数据库,这在设计上是不合理的。当然用递归读取树形的数据库效率是相当低的,你用多线程也不会很快。我曾经用的方法就是 在数据里面增加一个字段 这个字段就是路径字段 如:宇宙《银河系《太阳系《地球《中国《。 这样你搜索的是就按照这个这段来搜,相当的快 如你要找地球下面的所有的:select * from tablename where path like '宇宙《银河系《太阳系《地球%'. 这里有参考
    2.对于生成treeview问题 这里我建议不要一次生成完 这样数据量大的时候很容易造成界面假死的现象。我的思路是 最开始只显示节点下面的一层,当点击某个节点的时候在显示此节点的下一层。这样速度也很快,也不至于有死机的问题。
    参考代码:        #region TreeView AfterSelect Event.
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                this.getCon();
                string name = this.treeView1.SelectedNode.Text.ToString();
                this.treeView1.SelectedNode.Nodes.Clear(); // Clear the selected node's children nodes.            SortedList<string, int> childern = findUnder(name); // Find the child nodes from the data.            foreach (KeyValuePair<string, int> s in childern)
                {
                    if (data.Contains(s.Key)) // Check the srearch contain the nodes.
                    {
                         TreeNode child = new TreeNode(s.Key.ToString());
                         this.treeView1.SelectedNode.Nodes.Add(child); // Add the found nodes into the selected node.
                         data.Remove(s.Key);
                    }
                }
                for (int k = 0; k < data.Count; k++) 
                {
                    TreeNode child = new TreeNode(data[k].ToString());
                    this.treeView1.Nodes.Add(child);
                }
                this.treeView1.SelectedNode.Expand();
                this.showInfo(name); // Show the selected node's information.
                this.odcConnection.Close();// Close the data connection.
            }
    最后附上我的联系方式, 以后有机会大家一起探讨一下
    QQ:287072382
    MSN:[email protected]
      

  3.   

        #region TreeView AfterSelect Event.
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                this.getCon();
                string name = this.treeView1.SelectedNode.Text.ToString();
                this.treeView1.SelectedNode.Nodes.Clear(); // Clear the selected node's children nodes.            SortedList<string, int> childern = findUnder(name); // Find the child nodes from the data.            foreach (KeyValuePair<string, int> s in childern)
                {
                    if (data.Contains(s.Key)) // Check the srearch contain the nodes.
                    {
                         TreeNode child = new TreeNode(s.Key.ToString());
                         this.treeView1.SelectedNode.Nodes.Add(child); // Add the found nodes into the selected node.
                         data.Remove(s.Key);
                    }
                }
                for (int k = 0; k < data.Count; k++) 
                {
                    TreeNode child = new TreeNode(data[k].ToString());
                    this.treeView1.Nodes.Add(child);
                }
                this.treeView1.SelectedNode.Expand();
                this.showInfo(name); // Show the selected node's information.
                this.odcConnection.Close();// Close the data connection.
            }