如何在嵌套的TClientDataSet中找到“外键”字段名?

3

如何在嵌套的TClientDataSet中找到详细TClientDataSet上的链接字段名?

我正在将数据从一个TClientDataSet复制到另一个(逐条记录),我希望自动忽略链接字段。

我也可以使用TClientDataSet.Data属性复制数据,但仍需要清除链接和键字段。

1个回答

0
您可以检查详细/嵌套数据集的"DataSetField"属性,或访问主数据集的"NestedDataSet"属性。
以下是获取“link”字段名称的示例代码:
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;

附言:两年后我不认为这个答案会对问题的作者有所帮助,但或许可以帮助其他搜索同一主题的人。


很抱歉三年后才回复你的答案。但我正在寻找链接字段的名称,而不是_DataSetField_字段本身的名称。例如,在主细节关系中,如果在细节_DataSet_中有_ID、PARENT_ID,则我想找到_PARENT_ID_字段的名称。 - Valdir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接