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

发布于 2024-09-07 14:02:45 字数 183 浏览 0 评论 0原文

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

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

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

Given a nested TClientDataSet, how could I find the link field name on the detail TClientDataSet?

I'm copying data from one TClientDataSet to another (record by record) and I would like to automatically ignore the link field.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-09-14 14:02:45

您可以检查详细信息/嵌套数据集的“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;

PS:2年后,我不相信这个答案会对问题的作者有所帮助,但也许可以帮助搜索同一主题的其他人。

You can check the "DataSetField" property of the detail/nested dataset, or access the "NestedDataSet" property of the master 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;

Invoking example:

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

P.S.: 2 years later I don't believe this answer will help the author of the question, but maybe can help others that search for the same subject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文