【问题标题】:How to save and read records in a Delphi tClientDataset如何在 Delphi tClientDataset 中保存和读取记录
【发布时间】:2018-09-19 16:24:01
【问题描述】:

美好的一天

我是德尔福新手。我正在尝试以编程方式将记录保存到 tClientDataset,然后从数据集中读取记录。我想我似乎已经成功地将记录保存在数据集中,因为在我将数据附加到记录并发布后,数据集的记录数为 1。 但是,当我尝试读取记录的值时,我会返回 Null。 我使用工具栏创建了 tclientdataset,并在设计窗口中手动设置了字段。数据集的名称是

dsUnitData

我正在使用 Delphi RadStudio XE2。
谁能帮忙指出我在读取 Null 而不是之前填充的值时做错了什么?

这是我的代码:

procedure TfFeetRevenueByUnit.BitBtn1Click(Sender: TObject);
var test, theunitname : string;
count, feet, counter : integer;
revenue :currency;
begin
label3.Visible := false;

dsUnitData.Insert;
dsUnitData.FieldValues['Field_UnitName'] := 'test';
dsUnitData.FieldValues['Field_Feet'] := 10;
dsUnitData.FieldValues['Field_Revenue'] := 10.1;
dsUnitData.Post;

count := dsUnitData.RecordCount;

if not dsUnitData.Active then
dsUnitData.Open;
dsUnitData.First;
while not dsUnitData.EOF do
begin
theunitname := dsUnitData.FieldByName('Field_UnitName').Value;
feet := dsUnitData.FieldByName('Field_UnitName').Value;
revenue := dsUnitData.FieldByName('Field_Revenue').Value;
dsUnitData.Next;
end;

【问题讨论】:

  • 删除while not dsUnitData.Eof 循环和dsUnitData.Next。它们仅在您遍历多条记录并且只读取一条记录时使用。
  • 感谢您的快速回复。我确实删除了两行代码,但是,我仍然在以下行得到一个 Null 值:theunitname := dsUnitData.FieldByName('Field_UnitName').Value;

标签: delphi-xe2 tclientdataset


【解决方案1】:

我发现了问题。我错误地为 tClientDataset 设置了字段的 FieldKind。 我将 FieldKind 设置为 fkCalculated,但它应该是 fkInternalCalc

【讨论】:

  • 这些字段似乎不是其中任何一个。它们似乎是 fkData(指定类型的数据存储)。你真的应该阅读文档。
猜你喜欢
  • 1970-01-01
  • 2011-09-29
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多