olevariant序列TDATASET

使用OLEVARIANT可以序列任何类型的对象。下面的代码序列TDATASET。

procedure TForm1.DatasetToVariant(var varResultSet: OleVariant;
                                    ADataset : TDataset);
var
  m : Integer;
  nRecords, nColumns, nCurRec : Integer;
begin
  nRecords := -1;
  nColumns := -1;

  try
    { Create the array... }
    nColumns := Max(0, ADataset.FieldCount-1);

     nRecords := Max(0, ADataset.RecordCount);

    varResultSet := VarArrayCreate([0, nColumns, 0, nRecords],
                                   varVariant);

    for m := 0 to nColumns do
      varResultSet[m, 0] := ADataset.Fields[m].DisplayLabel;

       ADataset.First;
    nCurRec := 1;

    while not ADataset.Eof do begin
           for m := 0 to nColumns do
        varResultSet[m, nCurRec] := ADataset.Fields[m].Value;

      ADataset.Next;
      Inc(nCurRec);
    end;
  except
    on E: Exception do
      raise Exception.Create('CreateVarArrayFromDataset() - ' +
                              IntToStr(nRecords) +
                             ' rec,'+IntToStr(nColumns)
                             +'cols,'+E.Message);
  end;
end;

你可能感兴趣的:(Data)