You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何加载和保存三层ClientDataset中非顶层的嵌套数据?

Handling Nested ClientDataset Load/Save Without the Master Record

Hey there, let's break down exactly how to skip the top-level cdsMaster and work directly with your nested cdsTables and cdsNotes datasets. The core idea is that each nested dataset is its own TClientDataset instance, attached to a TDataSetField in the master—so we can interact with them directly.

Step 1: Get References to the Nested Datasets

First, you need to access the actual TClientDataset objects behind the nested fields in cdsMaster. These are exposed via the NestedDataSet property of the TDataSetField type fields:

var
  cdsNestedTables: TClientDataset;
  cdsNestedNotes: TClientDataset;
begin
  // Retrieve the nested datasets from the master's fields
  cdsNestedTables := TClientDataset(cdsMaster.FieldByName('cdsTables').NestedDataSet);
  cdsNestedNotes := TClientDataset(cdsMaster.FieldByName('cdsNotes').NestedDataSet);
end;

Make sure cdsMaster is open and has at least one record created (and posted, if needed) before accessing these—otherwise the nested datasets might not be initialized.

Step 2: Save the Nested Datasets

To save just the nested records, call SaveToFile directly on the nested dataset references, not the master:

// Save cdsTables data to a file
cdsNestedTables.SaveToFile('MyTablesData.cds', dfBinary);

// Save cdsNotes data to a file
cdsNestedNotes.SaveToFile('MyNotesData.cds', dfBinary);

You can use formats like dfBinary (default, compact) or dfXML (human-readable) depending on your needs.

Step 3: Load Data Into the Nested Datasets

When loading, again target the nested datasets directly. Optionally clear existing data first, then load from your saved files:

// Clear existing data in cdsTables (optional, based on your workflow)
cdsNestedTables.EmptyDataSet;
// Load saved data into the nested table
cdsNestedTables.LoadFromFile('MyTablesData.cds');

// Repeat for cdsNotes
cdsNestedNotes.EmptyDataSet;
cdsNestedNotes.LoadFromFile('MyNotesData.cds');

Once loaded, the nested data will automatically be associated with the current cdsMaster record—no need to touch the master dataset for this operation.

Key Notes to Remember

  • Ensure Master Record Exists: The nested datasets are tied to a specific record in cdsMaster. Make sure you've created and posted (if required) the master record before accessing or modifying the nested data.
  • Structure Consistency: When loading, the saved file's structure must match the nested dataset's structure (fields, data types, constraints). If you created the structure at runtime, this should be handled automatically as long as you use the same structure for save/load.
  • Edit/Browse State: If cdsMaster is in edit mode when you load the nested data, the changes will be part of the master record's pending edits. If it's in browse mode, the nested data will be saved immediately to the master's underlying data.

内容的提问来源于stack exchange,提问作者Ashlar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:26:52