从TdataSet生成OleVariant

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

  try
    { Create the array... }
    { Set size to 0..m-1 where m equals the number of columns. }
    nColumns := Max(0, ADataset.FieldCount-1);

    { Each item is an array of size (0..n) where n equals the }
    { number of records.}
    { Entry 0 is where we store the column name. }

    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;

    { Populate from result set. }
    ADataset.First;
    nCurRec := 1; { Current record number. }
    while not ADataset.Eof do begin
      { Put in field values. }
      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;

posted @ 2008-09-19 15:39  delphi中间件  阅读(217)  评论(0编辑  收藏  举报