创建临时表

                            创建临时表字段函数 
function TForm1.CreateTables(const tempFieldDefs:TFieldDefs):TDataSet; 
var 
TempTable:TClientDataSet; 
begin 
 TempTable:=nil; 
 Result:=nil; 
 if tempFieldDefs<>nil then 
 begin 
 try 
   TempTable:=TClientDataSet.Create(Application); 
   TempTable.FieldDefs.Assign(tempFieldDefs); 
   TempTable.CreateDataSet; 
   Result:=(TempTable as TDataSet); 
 Except 
   if TempTable<>nil then 
   begin 
     TempTable.Free; 
     raise; 
   end; 
 end 
 end 
end; 

//创建临时表 
procedure TForm1.Button1Click(Sender: TObject); 
var 
 TempDataSet :TDataSet; 
begin 
 TempDataSet:=TDataSet.Create(Self); 
 with TempDataSet.FieldDefs do 
 begin 
   Add('stringvalue',ftString,20,False); 
   Add('integervalue',ftInteger,0,False); 
   Add('floatvalue',ftFloat,0,false); 
 end; 
 with DataSource1 do 
 begin 
   DataSet:=CreateTables(TempDataSet.FieldDefs); 
   DataSet.Open; 
 end; 
 TempDataSet.Free; 
end; 

//使用临时表数据 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  with datasource1 do 
  begin 
    Dataset.Append; 
    Dataset.FieldValues['stringvalue'] := 'test'; 
    Dataset.FieldValues['integervalue'] := 13; 
    Dataset.FieldValues['floatvalue'] := 3.4; 
    Dataset.Post; 
  end; 
end;
                                

查看回复