How to find the name of a foreign key field on a nested TClientDataSet?

Given a nested TClientDataSet, how can I find the link field name in a detailed TClientDataSet?

I copy data from one TClientDataSet to another (write-by-write), and I would like to automatically ignore the link field.

I could also copy the data using the TClientDataSet.Data property, but I still need to clear the link and key fields.

+3
source share
1 answer

You can check the DataSetField property for a detailed / nested dataset or access the "NestedDataSet" property of the main dataset.

Sample code to get the link field name:

function GetCDSDLinkFieldName(cds: TClientDataSet): string;
var
  i: Integer;
  cdsDetail: TClientDataSet;
begin
  Result := EmptyStr;
  cdsDetail := nil;
  if Assigned(cds.DataSetField) then
    cdsDetail := cds;
  if not Assigned(cdsDetail) and (cds.FieldCount > 0) then
  begin
    i := 0;
    while not Assigned(cdsDetail) and (i < cds.FieldCount) do
    begin
      if cds.Fields[i].DataType = ftDataSet then
        cdsDetail := TClientDataSet(TDataSetField(cds.Fields[i]).NestedDataSet);
      Inc(i);
    end;
  end;
  if Assigned(cdsDetail) then
    Result := cdsDetail.DataSetField.FieldName;
end;

:

procedure ...
begin
  ShowMessage(GetCDSDLinkFieldName(cdsMaster));
  ShowMessage(GetCDSDLinkFieldName(cdsDetail));
end;

P.S.: 2 , , , .

0

Source: https://habr.com/ru/post/1752574/


All Articles