Retrieving Result Sets with Commands

From RAD Studio
Jump to: navigation, search

Go Up to Using Command Objects


Unlike TADOQuery components, which use different methods to execute depending on whether they return a result set, TADOCommand always uses the Execute command to execute the command, regardless of whether it returns a result set. When the command returns a result set, Execute returns an interface to the ADO _RecordSet interface.

The most convenient way to work with this interface is to assign it to the RecordSet property of an ADO dataset.

For example, the following code uses TADOCommand (ADOCommand1) to execute a SELECT query, which returns a result set. This result set is then assigned to the RecordSet property of a TADODataSet component (ADODataSet1).

with ADOCommand1 do begin
  CommandText := 'SELECT Company, State ' +
    'FROM customer ' +
    'WHERE State = :StateParam';
  CommandType := cmdText;
  Parameters.ParamByName('StateParam').Value := 'HI';
  ADODataSet1.Recordset := Execute;
end;
ADOCommand1->CommandText = "SELECT Company, State ";
ADOCommand1->CommandText += "FROM customer ";
ADOCommand1->CommandText += "WHERE State = :StateParam";
ADOCommand1->CommandType = cmdText;
ADOCommand1->Parameters->ParamByName("StateParam")->Value = "HI";
ADOCommand1->Recordset = ADOCommand1->Execute();

As soon as the result set is assigned to the ADO dataset's Recordset property, the dataset is automatically activated and the data is available.

See Also