Title: Pass a recordset from a COM object to ASP VBScript
Question: How to return a resultset (dataset) from COM to ASP
Answer:
the Delphi code:
uses
ADOInt;
...
function ASPObj.GetRS: OleVariant;
var
ADODataSet1: TADODataSet;
RecordSet : _Recordset;
begin
ADODataSet1 := TADODataSet.Create(nil);
try
ADODataSet1.ConnectionString := Provider;
ADODataSet1.CommandText := 'SELECT * FROM ' + TabName;
ADODataSet1.Open;
RecordSet := ADODataSet1.Recordset._xClone;
RecordSet.Set_ActiveConnection(nil);
Result := RecordSet;
ADODataSet1.Close;
finally
ADODataSet1.Free;
end;
end;
from the Delphi help (why to call Free not Destroy):
Do not call Destroy directly in an application. Usually destruction of ADO query components is handled automatically by Delphi. If an application creates its own instances of a query, and does not assign an Owner that is responsible for freeing the dataset, then the application should call Free, which checks that the query is not nil before calling Destroy.
the ASP VBScript code:
Dim DelphiASPObj
Dim RS
Set DelphiASPObj = Server.CreateObject("ActiveXDLL.ASPObj")
Set RS = DelphiASPObj.GetRS
RS.MoveFirst
do until RS.EOF
for each field in RS.fields
Response.Write field.value & " "
next
Response.Write ""
RS.MoveNext
loop
...
Set DelphiASPObj = Nothing
Set RS = Nothing