Create Table DEPT (DEPTID char(10),DEPTMC char(10)) go Insert Into DEPT SELECT '001','业务部' Union All Select '002','开发部' union all select '003','测试部' go create table employee(ID char(10),deptid char(10),name(10)) go insert into employee select '0001','001','张三' union all select '0002','001','李四' union all select '0003','002','马五' union all select '0004','003','赵六' go//以下是Delphi代码 procedure TForm1.Button1Click(Sender: TObject); var vsl:TStringList; stream_temp: TStringStream; begin ADOQ_DEPT.SQL.Text:='Select * From DEPT'; ADOQ_DEPT.Open; vsl:=TStringList.Create; vsl.Add('部门'); while not ADOQ_DEPT.Eof do begin vsl.Add(#9+ADOQ_DEPT.FieldByName('DEPTMC').AsString); ADOQ_employee.Close; ADOQ_employee.SQL.Text:='Select * From employee Where DEPTID='+QuotedStr(ADOQ_DEPT.FieldByName('deptid').AsString); ADOQ_employee.Open; while not ADOQ_employee.Eof do begin vsl.Add(#9#9+ADOQ_employee.FieldByName('NAME').AsString); ADOQ_employee.Next; end; ADOQ_DEPT.Next; end; stream_temp:=TStringStream.Create(vsl.Text); TreeView1.LoadFromStream(stream_temp); end;
go
Insert Into DEPT
SELECT '001','业务部' Union All
Select '002','开发部' union all
select '003','测试部'
go
create table employee(ID char(10),deptid char(10),name(10))
go
insert into employee
select '0001','001','张三' union all
select '0002','001','李四' union all
select '0003','002','马五' union all
select '0004','003','赵六'
go//以下是Delphi代码
procedure TForm1.Button1Click(Sender: TObject);
var vsl:TStringList;
stream_temp: TStringStream;
begin
ADOQ_DEPT.SQL.Text:='Select * From DEPT';
ADOQ_DEPT.Open;
vsl:=TStringList.Create;
vsl.Add('部门');
while not ADOQ_DEPT.Eof do
begin
vsl.Add(#9+ADOQ_DEPT.FieldByName('DEPTMC').AsString);
ADOQ_employee.Close;
ADOQ_employee.SQL.Text:='Select * From employee Where DEPTID='+QuotedStr(ADOQ_DEPT.FieldByName('deptid').AsString);
ADOQ_employee.Open;
while not ADOQ_employee.Eof do
begin
vsl.Add(#9#9+ADOQ_employee.FieldByName('NAME').AsString);
ADOQ_employee.Next;
end;
ADOQ_DEPT.Next;
end;
stream_temp:=TStringStream.Create(vsl.Text);
TreeView1.LoadFromStream(stream_temp);
end;