procedure TfrmMain.Button1Click(Sender: TObject);var
  MyQuery1,MyQuery2 , MyQuery3,MyQuery4  : TAdoQuery ;
  sqlCode1 ,sqlCode2 ,sqlCode3 ,sqlcode4 : string;
  strTmp1,strTmp3,strTmp4,strTmp5: string;
  RootNode1, subNode1 ,subNode2 , subNode3 : TTreeNode;
  P1: PString;
begin
  strTmp2 :='';
  try
    MyQuery1 := TAdoQuery.create(nil);
    with MyQuery1 do
    begin
      Connection := AdoCOn;
      if active then close;
      sql.clear;
      sqlCOde1 := ' set rowcount 0 select distinct department,department_name from run..department '
                + ' where  len(department)=3' ;
      sql.add(sqlcode1);
      open;
      if not MyQuery1.IsEmpty then
      begin
        tv_zb.Items.Clear;
        tv_zb.Items.BeginUpdate;
        New(P1);
        strTmp1 := FieldByname('department_name').AsString  ;
        strTmp2 := FieldByname('department').AsString  ;
        RootNode1 := tv_zb.Items.AddObject(Nil, 'strTmp1', p1);
        RootNode1.ImageIndex := 19;
        MyQuery1.next;
        end;
        tv_zb.Items.EndUpdate;
      end;
  except
    myQuery1.Free;
  end;  try
    MyQuery2 := TAdoQuery.create(nil);
    with MyQuery2 do
    begin
      Connection := AdoCOn;
      if active then close;
      sql.clear;
      SQLCode2 := ' Select Distinct department, department_name From dun..department'
                + ' Where department Like ' + QuotedStr( StrTmp2 ) + '%'+' And Len( department ) = '
                + ' (Len( StrTmp2 ) + 3 )';
      sql.add(sqlcode2);
      open;
      if not MyQuery2.IsEmpty then
      begin
        tv_zb.Items.BeginUpdate;
        strTmp3 := FieldByname('department_name').AsString  ;
        strTmp2 := FieldByname('department').AsString  ;
        subNode1 := tv_zb.Items.AddchildObject(RootNode1, strTmp3,nil);
        subNode1.ImageIndex := 19;
        MyQuery2.next;
        end;
        tv_zb.Items.EndUpdate;
      end;
  except
    myQuery2.Free;
  end;  try
    MyQuery3 := TAdoQuery.create(nil);
    with MyQuery3 do
    begin
      Connection := AdoCOn;
      if active then close;
      sql.clear;
      SQLCode2 := ' Select Distinct department, department_Name From run..department'
                + ' Where department Like ' + QuotedStr( StrTmp2 ) + '%'+' And Len( department) = '
                + '( Len( StrTmp2 ) + 3 )';
      sql.add(sqlcode3);
      open;
      if not MyQuery3.IsEmpty then
      begin
        tv_zb.Items.BeginUpdate;
        strTmp4 := FieldByname('department_name').AsString  ;
        strTmp2 := FieldByname('department').AsString  ;
        subNode2  := tv_zb.Items.AddchildObject(subNode1, strTmp4,nil);
        subNode2.ImageIndex := 19;
        MyQuery3.next;
        end;
        tv_zb.Items.EndUpdate;
      end;
  except
    myQuery3.Free;
  end;  try
    MyQuery3 := TAdoQuery.create(nil);
    with MyQuery4 do
    begin
      Connection := AdoCOn;
      if active then close;
      sql.clear;
      SQLCode2 := ' Select Distinct department, department_Name From run..department'
                + ' Where department Like ' + QuotedStr( StrTmp2 ) + '%'+' And Len(department) = '
                + '( Len( StrTmp2 ) + 3 )';
      sql.add(sqlcode4);
      open;
      if not MyQuery4.IsEmpty then
      begin
        tv_zb.Items.BeginUpdate;
        strTmp5 := FieldByname('department_name').AsString  ;
        strTmp2 := FieldByname('department').AsString  ;
        subNode3 := tv_zb.Items.AddchildObject(subNode2, strTmp5,nil);
        subNode3.ImageIndex := 19;
        MyQuery4.next;
        end;
        tv_zb.Items.EndUpdate;
      end;
  except
    myQuery4.Free;
  end;end;
其中strTmp2为全局变量!
为什么可以编译通过而不生成树
我是刚学不久的,以前从来没有建过树,请各位大侠帮帮我啊!万分感谢!

解决方案 »

  1.   

    为什么啊?csdn上都没有肯帮我吗?有那位懂的帮帮我吧!
      

  2.   

    看的头晕目眩,发现一错误:
    第四个try中写错了一个
    MyQuery3 := TAdoQuery.create(nil);
    应该是MyQuery4 := TAdoQuery.create(nil);
      

  3.   

    又发现两错误:
    第三和四中的
    SQLCode2 := ' Select Distinct ....
    改为
    SQLCode3 := ' Select Distinct ....
    SQLCode4 := ' Select Distinct ....
    拜托,楼主,以后自己先检查一下再到这来问!
      

  4.   

    所有的   +'( Len( StrTmp2 ) + 3 )'
    改为   +'( Len( '''+StrTmp2 +''') + 3 )'