Question

Currently I have followed the example present in the documentation.

Specifically the example named:

Example: Stored Procedure Returning Multiple Rowsets

This is the delphi code translated from the above example:

//Copied from ADODB.pas
function CreateADOObject(const ClassID: TGUID): IUnknown;
var
  Status: HResult;
  FPUControlWord: Word;
begin
  asm
    FNSTCW  FPUControlWord
  end;
  Status := CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or
    CLSCTX_LOCAL_SERVER, IUnknown, Result);
  asm
    FNCLEX
    FLDCW FPUControlWord
  end;
  if (Status = REGDB_E_CLASSNOTREG) then
    raise Exception.CreateRes(@SADOCreateError) else
    OleCheck(Status);
end;

var            
  Con: TADOConnection;
  RSet: _Recordset;
  Cmd: _Command;
  P1, P2: _Parameter;
  i: integer;

  RecordsAffected: OleVariant;
begin
  Con := TADOConnection.Create(nil);
  try
    Con.ConnectionString := PromptDataSource(Handle, '');
    Con.Open('user', 'password');

    Cmd := CreateADOObject(CLASS_Command) as _Command;
    try
      Cmd.Set_ActiveConnection(Con.ConnectionObject);

      P1 := Cmd.CreateParameter('P1', adSmallInt, adParamInput, 0, 1);
      Cmd.Parameters.Append(P1);
      P2 := Cmd.CreateParameter('P2', adSmallInt, adParamOutput, 0, EmptyParam);
      Cmd.Parameters.Append(P2);

      Cmd.Properties['PLSQLRSet'].Value := True;
      Cmd.CommandType := adCmdText;
      Cmd.CommandText := '{CALL Employees.GetEmpRecords(?, ?)}';

      RSet:= CreateADOObject(CLASS_Recordset) as _RecordSet;
      try    
        //If I use Execute, the CursorLocation will be adUseServer
        RSet.Open(Cmd, EmptyParam, adUseClient, adOpenStatic, adCmdText);

        ShowMessage(IntToStr(RSet.RecordCount));
        for I:= 0 to RSet.Fields.Count-1 do
        begin
          showmessage(vartostr(RSet.Fields.Item[i].Name)+': '+vartostr(RSet.Fields.Item[i].Value));
        end;

        Cmd.Properties['PLSQLRSet'].Value := False;  
      finally
        RSet := nil;
      end;

    finally
      Cmd := nil;
    end;

  finally
    Con.Free;
  end;
end;

NOTE: I changed the procedure to return only one cursor!


Question:

1.a) How Can I fill a DataSet from the recordset?

Or

1.b) Is there another way to capture out cursor parameters from an oracle procedure through ADO in Delphi?


UPDATE 1:

As suggested by MartynA I tried to use TADQuery, but unsuccessfully so far:

object ADOQuery1: TADOQuery
    Connection = ADOConnection1
    CursorType = ctStatic
    Parameters = <
      item
        Name = 'param0'
        DataType = ftUnknown
        Direction = pdOutput
        Size = -1
        Value = Null
      end
      item
        Name = 'param1'
        DataType = ftSmallint
        Size = -1
        Value = 7084
      end
      item
        Name = 'param2'
        DataType = ftSmallint
        Direction = pdOutput
        Size = -1
        Value = Null
      end>
    SQL.Strings = (
      '{CALL Employees.GetEmpRecords(:param0, :param1, :param2)}')
    Left = 560
    Top = 96
  end


procedure TForm1.ADOConnection1WillExecute(Connection: TADOConnection;
  var CommandText: WideString; var CursorType: TCursorType;
  var LockType: TADOLockType; var CommandType: TCommandType;
  var ExecuteOptions: TExecuteOptions; var EventStatus: TEventStatus;
  const Command: _Command; const Recordset: _Recordset);
begin
  CommandType := cmdText;
  Command.Properties['PLSQLRSet'].Value := True;
end;
Was it helpful?

Solution

You can assign directly your recordset to an ADODataset

ADODataset := TADODataset.Create(nil);
ADODataset.Recordset := RecordsetData;

OTHER TIPS

It is very simple to answer the first :

    RSet.Open(Cmd, EmptyParam, adUseClient, adOpenStatic, adCmdText);

    DataSet := TADOQuery.Create(nil);
    try
      DataSet.Recordset := RSet;
      ...

When you 'SET' the RecordSet into the DataSet, it is automatically filled with recordset data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top