Using Locate method when fetchAll=false
Posted: Fri 02 Mar 2012 12:32
Hello.
It is Required to search the dataset record by unique field (TGuidField type) when FetchAll mode is false;
But according to the description, first calling Locate negates all the advantages of the "fetchall=false".
Here's:
Everething is ok in test application (only 1 form, 1 connect, 1 dataset), but in real app - Access violation (Marshal.PtrToStringUni).
May be you can help... thx
_______________________________________________
Delphi XE 15.0.3953.35171
SDac 6.0.2
It is Required to search the dataset record by unique field (TGuidField type) when FetchAll mode is false;
But according to the description, first calling Locate negates all the advantages of the "fetchall=false".
This is not acceptable. I tried to write my own decision based on code of Locate method.When the FetchAll property is False, the first call to the TMemDataSet.Locate and TMemDataSet.LocateEx methods may take a lot of time to retrieve additional records to the client side. The default value is True.
Here's:
Code: Select all
type
TMSQueryProtect = class(TMSQuery);
function LocateFetched(Query : TMSQuery; KeyField : string; Value : Variant): boolean;
var
CurrentRecNumber : Integer;
RecBuf: IntPtr;
i : Integer;
FieldOffset : Integer;
ptr : Pointer;
begin
Result := False;
with TMSQueryProtect(Query).Data do
if RecordCount>0 then
begin
Result := False;
CurrentRecNumber := RecordNo;
RecordNo := 1;
RecBuf := AllocRecBuf(RecBuf);
try
FieldOffset := FieldByName(KeyField).Offset;
for I := 1 to RecordCount do
begin
GetRecord(RecBuf);
if Marshal.PtrToStringUni(Marshal.ReadIntPtr(PtrOffset(RecBuf, FieldOffset)))=Value then
begin
Query.Resync([rmExact, rmCenter]);
Result := true;
Break;
end;
RecordNo:= RecordNo+1;
end;
if not Result then
RecordNo := CurrentRecNumber;
finally
FreeRecBuf(RecBuf);
end;
end;
end;
May be you can help... thx
_______________________________________________
Delphi XE 15.0.3953.35171
SDac 6.0.2