给出一段试用的程序,
先生成一个query1,order by name
在利用递归生成一个tclientdataset,用来存放排需后的数据集
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, Grids, DBGrids,dbclient;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
DataSource2: TDataSource;
Query1: TQuery;
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
tcdsut:TClientDataSet;
procedure sortbyid(id:integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure Tform1.sortbyid(id:integer);
var icount :integer;
nrecno:integer;
begin
query1.First;
for icount := 0 to query1.RecordCount -1 do
begin
if (query1.Fields[2].value= id) then
begin
tcdsut.append;
tcdsut.Fields[0].value := query1.fields[0].value;
tcdsut.fields[1].value := query1.fields[1].value;
tcdsut.fields[2].value := query1.fields[2].value;
nRecno := query1.recordcount - query1.RecNo;
sortbyid(query1.fields[0].value);
query1.MoveBy(-nRecno);
end;
query1.next;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var icount:integer;
strsql ,strcondition:string;
curid:integer;
nrecno :integer;
begin
// add fieldsname
tcdsut.Close;
tcdsut.FieldDefs.Clear;
tcdsut.FieldDefs.Add('id',ftinteger,0,false);
tcdsut.FieldDefs.add('name',ftstring,10,false);
tcdsut.FieldDefs.Add('parent',ftinteger,0,false);
//add data
tcdsut.CreateDataSet;
tcdsut.Open;
query1.First;
sortbyid(-1); // -1 是最上一层的PARENT字段值
datasource2.DataSet := tcdsut;
dbgrid2.DataSource := datasource2;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
tcdsut := TClientDataset.Create(application);
end;
end.
TO: 学生
connect by如何用,我在isql的帮助理查询不到,谢谢 !