Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting TParams to ADO TParameters 1

Status
Not open for further replies.

ehamlin

Programmer
Aug 5, 2004
2
US
Having some trouble with this...need to have my app work with both ADO and non-ADO datasets, so I need a conversion function that maps a standard VCL TParams list to an ADO TParameters. I keep getting typecast errors but can't see why. Here's the code...any hints? TIA

----------------------------
function TDBServer.ConvertToADOParms(Owner: TADODataset; aParams: TParams): TParameters;
var
aSize,i: integer;
Dir: TParameterDirection;
aResult : TParameters;
begin

// Convert a standard TParams object to an ADO-specific TParameters object

try
aResult :=nil;
Result :=aResult;
if aParams = nil then exit;

aResult :=TParameters.create( Owner, TParameter);

for i:=0 to aParams.count - 1 do
begin
if aParams = nil then
continue;

aSize :=aParams.size;

case aParams.ParamType of
ptUnknown:
Dir :=pdUnknown;

ptInput:
begin
Dir :=pdInput;
if aParams.size < length(aParams.value) then
aSize :=length(aParams.value);
end;

ptOutput:
Dir :=pdOutput;

ptInputOutput:
Dir :=pdInputOutput;

ptResult:
Dir :=pdReturnValue;
else
Dir :=pdInput;
end;

with aResult.AddParameter do
begin
Name := widestring(aParams.Name);
Datatype :=aParams.DataType;
Direction :=Dir;
Size :=aSize;
Value :=aParams.value;

end;

end;

Result :=aResult;

except
on e:exception do
begin
showmessage('Could not convert standard parameter object to ADO parameter object: '+
e.message);
aResult :=nil;
exit;
end;

end;
end;
 
played around a bit with your code and this works fine for me :

Code:
function ConvertToADOParms(Owner: TADODataset; aParams: TParams): TParameters;

var i: integer;

begin
// Convert a standard TParams object to an ADO-specific TParameters object
 Result :=nil;
 try
  if aParams = nil then exit;
  Result :=TParameters.create( Owner, TParameter);
  for i:=0 to aParams.count - 1 do
   begin
    if aParams[i] = nil then continue;
    with Result.AddParameter do
     begin
      Name := aParams[i].Name;
      Datatype :=aParams[i].DataType;
      Direction :=TParameterDirection(aParams[i].ParamType);
      Size :=aParams[i].size;
      Value :=aParams[i].value;
     end;
   end;
 except
  on e:exception do
    begin
     Result :=nil;
     showmessage('Could not convert standard parameter object to ADO parameter object: '+e.message);
    end;
 end;
end;

--------------------------------------
What You See Is What You Get
 
Wow, thanks. Didn't expect hand-edited code. That's really generous of you. Will try it out as soon as I can focus on it once more.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top