J
jadesun
Unregistered / Unconfirmed
GUEST, unregistred user!
在做无限级菜单时,用以下的代码为什么会占用大量的资源呢?虽然用的是递归,但也不至于这样啊。应该怎么改呢?
表里面的资料很简单
MenuID ParentID MenuName
1 1 delphi
2 1 数据库-文件型
3 1 数据库-C/S型
4 2 控件 - 开发
5 4 控件 - 使用
6 3 OLE/Automation
7 6 ActiveX 控件
8 1 局域网 / 通讯
9 8 Internet/TCPIP
10 1 系统相关
11 9 IDE/使用技巧
12 10 多媒体
13 10 Windows API
procedure TForm1.FormCreate(Sender: TObject);
Var MyRecordPointerMyRecord;
Begin
OpenDataBase();
New(MyRecordPointer);
MyRecordPointer.MenuID := 1;
MyRecordPointer.ParentID := 1;
MyRecordPointer.MenuName := 'BJAM文案管理';
TreeView1.Items[0].Data := MyRecordPointer;
CreateSubNodes(TreeView1.Items[0]);
end;
Procedure TForm1.CreateSubNodes(MyNode:TTreeNode);
Var
MyRecordPointerMyRecord;
SQLStr:String;
TempNode:TTreeNode;
rs:OleVariant;
i:Integer;
Begin
rs := CreateOleObject('ADODB.Recordset');
rs.ActiveConnection := Conn;
SQLStr := 'Select MenuID,ParentID,MenuName From [Menu] Where ParentID = '+IntToStr(PMyRecord(MyNode.Data)^.MenuId)+'';
rs.Open (SQLStr,Conn,1,1,1);
While Not rs.Eof Do
Begin
TempNode := TreeView1.Items.AddChild(MyNode,rs.fields['MenuName'].value);
New(MyRecordPointer);
MyRecordPointer.MenuID := rs.fields['Menuid'].value;
MyRecordPointer.ParentID := rs.fields['ParentID'].value;
MyRecordPointer.MenuName := rs.fields['MenuName'].value;
TempNode.Data := MyRecordPointer;
CreateSubNodes(TempNode);
rs.movenext;
End;
rs.close;
End;