给定一个记录集,如何填充TDataSet后代,以便以后填充网格?

时间:2014-02-20 16:24:14

标签: oracle delphi oledb ado procedure

目前,我已按照documentation中的示例进行操作。

特别是名为:

的例子
  

示例:存储过程返回多个行集

这是从上例中翻译的delphi代码:

//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;

注意:我更改了程序只返回一个光标!


问题:

1.a)如何从记录集中填充DataSet?

或者

1.b)还有另一种方法可以通过Delphi中的ADO从oracle过程中捕获游标参数吗?


更新1:

根据MartynA的建议,我尝试使用TADQuery,但到目前为止没有成功:

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;

2 个答案:

答案 0 :(得分:2)

您可以直接将记录集分配给ADODataset

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

答案 1 :(得分:0)

回答第一个问题非常简单:

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

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

当您将RecordSet“设置”到DataSet中时,它会自动填充记录集数据。