【问题标题】:How to find the "foreign key" field name on a nested TClientDataSet?如何在嵌套的 TClientDataSet 上找到“外键”字段名称?
【发布时间】:2010-07-01 04:18:39
【问题描述】:

给定一个嵌套的 TClientDataSet,我如何在细节 TClientDataSet 上找到链接字段名称?

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

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

【问题讨论】:

    标签: delphi tclientdataset midas-server


    【解决方案1】:

    您可以查看详细/嵌套数据集的“DataSetField”属性,或访问主数据集的“NestedDataSet”属性。

    获取“链接”字段名称的示例代码:

    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 年后我不相信这个答案会帮助问题的作者,但也许可以帮助其他搜索相同主题的人。

    【讨论】:

    • 很抱歉需要三年时间来评论您的答案。但我正在寻找链接字段的名称,而不是 DataSetField 字段名称本身。例如,在详细 DataSet 中与 ID, PARENT_ID 的主从关系中,我想找到 PARENT_ID 字段名称。
    猜你喜欢
    • 1970-01-01
    • 2018-03-12
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2015-05-18
    • 2011-05-09
    • 2021-06-08
    相关资源
    最近更新 更多