代码:
数据库字段: nodeid,title,parentid,turn
procedure TForm1.ReadTree(Node:TTreeNode;nid:Integer);
var
sql:string;
tNode:TTreeNode;
id:integer;
ado:TADOQuery;
begin
sql:=Format('select * from ttree where parentid=%d order by turn',[nid]);
ado:=TADOQuery.Create(nil);
ado.Connection:=ADOConnection1;
ado.SQL.Clear;
ado.SQL.Add(sql);
ado.Open;
ado.First;
while not ado.Eof do
begin
id:= ado.Fields[0].AsInteger;
tnode:=TreeView1.Items.AddChild(Node,ado.Fields[1].AsString ) ;
ReadTree(tNode,id);
ado.Next;
end;
ado.Close;
ado.Free;
end;
procedure TForm1.AddRoot;
var
ado:tadoquery;
node:TTreeNode;
begin
ado:=TADOQuery.Create(self);
ado.Connection:=ADOConnection1;
ado.SQL.Clear;
ado.SQL.Add('select * from ttree where parentid=0');
ado.Open;
while not ado.Eof do
begin
node:=TreeView1.Items.AddChild(nil,ado.Fields[1].AsString );
ReadTree(node,ado.Fields[0].AsInteger );
ado.Next;
end;
ado.Close;
ado.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AddRoot;
end;
启动速度很慢,CPU也100%,有办法优化或其他的方法实现吗??