以下代码生成行政区划代码树,但速度太慢,请高手优化。数据表和样例如下图:
private void textBox7_DoubleClick(object sender, EventArgs e)
{
OleDbConnection oleCon = getConnection();
oleCon.Open();
OleDbCommand oleCom = new OleDbCommand("select * from sysCode where CodeID=Parent and TypeId='AB'", oleCon);
OleDbDataReader dr = oleCom.ExecuteReader();
while (dr.Read())
{
OleDbCommand oleCom1 = new OleDbCommand("select * from sysCode where CodeID <> Parent and Parent='" + dr[1].ToString() + "'and TypeId='AB'", oleCon); OleDbDataReader dr1 = oleCom1.ExecuteReader();
TreeNode newNode1 = treeView1.Nodes.Add(dr[1].ToString(), dr[2].ToString());
while (dr1.Read())
{
TreeNode newNode2 = newNode1.Nodes.Add(dr1[1].ToString(), dr1[2].ToString());
OleDbCommand oleCom2 = new OleDbCommand("select * from sysCode where CodeID = Child and Parent='" + dr1[1].ToString() + "'and TypeId='AB'", oleCon);
OleDbDataReader dr2 = oleCom2.ExecuteReader();
while (dr2.Read())
{
newNode2.Nodes.Add(dr2[1].ToString(), dr2[2].ToString());
}
dr2.Close(); }
dr1.Close();
}
treeView1.CollapseAll();
dr.Close();
oleCon.Close();
treeView1.Visible = true; }http://album.alumni.163.com/uploadnew/30/25/81/812530/1222354252483.jpg
private void textBox7_DoubleClick(object sender, EventArgs e)
{
OleDbConnection oleCon = getConnection();
oleCon.Open();
OleDbCommand oleCom = new OleDbCommand("select * from sysCode where CodeID=Parent and TypeId='AB'", oleCon);
OleDbDataReader dr = oleCom.ExecuteReader();
while (dr.Read())
{
OleDbCommand oleCom1 = new OleDbCommand("select * from sysCode where CodeID <> Parent and Parent='" + dr[1].ToString() + "'and TypeId='AB'", oleCon); OleDbDataReader dr1 = oleCom1.ExecuteReader();
TreeNode newNode1 = treeView1.Nodes.Add(dr[1].ToString(), dr[2].ToString());
while (dr1.Read())
{
TreeNode newNode2 = newNode1.Nodes.Add(dr1[1].ToString(), dr1[2].ToString());
OleDbCommand oleCom2 = new OleDbCommand("select * from sysCode where CodeID = Child and Parent='" + dr1[1].ToString() + "'and TypeId='AB'", oleCon);
OleDbDataReader dr2 = oleCom2.ExecuteReader();
while (dr2.Read())
{
newNode2.Nodes.Add(dr2[1].ToString(), dr2[2].ToString());
}
dr2.Close(); }
dr1.Close();
}
treeView1.CollapseAll();
dr.Close();
oleCon.Close();
treeView1.Visible = true; }http://album.alumni.163.com/uploadnew/30/25/81/812530/1222354252483.jpg
http://album.alumni.163.com/uploadnew/30/25/81/812530/1222354222809.jpg
http://album.alumni.163.com/uploadnew/30/25/81/812530/1222354252483.jpg
如果觉得treeview速度慢的话,是否可以在内存中另外使用一个速度快的存储结构呢?然后在这个存储结构和treeview之间建立数据的映射。
但是,数据结构、算法、网络技术、数据库技术等等这些基础知识,却是构建起软件帝国的基石,是软件帝国得以运转的核心,只有掌握了这些,才能达到浪翻云和庞班这种上窥天道的境界。我是没有资格说这些话的,因为我毕竟不是这个圈子里面的人,也没有这方面的实践经验,但是从我所学的东西来看,这个方法似乎是到处都存在的:CPU中,为了解决内存读取速度和CPU运算速度之间的巨大差距,设置了一级、二级缓存,你可以将它想象成:将硬盘中100%的数据读取50%到内存,再读取20%到二级缓存,再读取5%到一级缓存,这个是稍微了解CPU硬件的人都会知道的。这种缓存机制,不仅在CPU中存在,在硬盘、光驱等硬件中存在,在操作系统中也是存在的,比如:windows的临时交换文件,还有现在vista的ReadyBoost技术和Superfetch技术。不仅如此,软件工程中,三层的架构设计也带有一点“缓存机制”的味道,比如:数据操作层就是底层的数据库、文件等数据与业务对象层之间的缓冲区,无论下面怎么变,只要对上面的接口不变,就不会影响上面的业务对象层的运作。诸如此类的例子还有很多,也不仅仅在软件方面,或计算机领域。小到橡皮垫、弹簧,大到舰载机上的阻拦索和钩子、航天器的对接缓冲,都会使用到这种“缓存机制”或“缓冲机制”,其本质,就是将两个不同速度、不同能量、不同吞吐量的事物或事务平滑的对接起来。所以,如果你觉得treeview速度很慢,不能满足你的需要,但又不想放弃这个控件的话,那么可以自己做一个能够快速定位读取的存储结构,将硬盘中读取来的数据存放到这个存储结构中,然后在treeview中直接调用这个存储结构中的数据,也就是说,让这个存储结构起到缓冲的作用。我想,这个思路,其实,用j2ee和c#都是可以实现的,不是吗?