如何实现“可写计算” TDataSet 中的字段?
我需要向 TDataSet 添加其他字段,这些字段不存在于基础数据库中,但可以从现有字段派生。我可以轻松地使用计算字段来完成此操作,并且效果非常好。
现在我想编辑这些字段并将更改后的数据写回。我可以反转计算以将数据写回现有字段,但数据库控件不允许我编辑计算字段。
有什么方法可以让我做到这一点吗?
更新: 好的,关于背景的更多细节。
数据集有一个 blob 字段,它是 TBytes 表示形式。某些字节被识别为包含可以使用现有数据库编辑字段以方便的方式表示的信息。不过,并非所有字节都是已知的,因此必须保持 TBytes 表示形式不变,以便通过另一个了解它的应用程序进行处理。该应用程序还修改现有记录并插入新记录。
数据集中不同记录的 TBytes 通常映射到不同的字段表示,尽管在数据集上设置过滤器或范围将确保它们具有相同的映射。
正如我所说,提取已知字节并通过计算字段将其转换为字符串、日期、数字等是没有问题的。也可以将这些值重新转换为 TByte。问题是使这些额外的字段可编辑,同时保持数据集导航完整。
如果有帮助:我们有执行双向映射的类,将字段公开为已发布的属性。
I am in the need to add additional fields to a TDataSet that don't exist in the underlying database but can be derived from existing fields. I can easily do this with caclulated fields and that works perfectly.
Now I want to edit these fields and write the changed data back. I can reverse the calculation to write the data back into the existing fields, but the DB controls just don't let me edit calculated fields.
Is there any approach that allows me to do this?
Update:
Ok, some more details about the background.
The dataset has a blob field, which is a TBytes representation. Some of the bytes are identified to contain information that can be represented in a convenient way with existing DB edit fields. Not all of the bytes are known, though, so the TBytes representation has to be kept as it is for processing through another application that knows about it. This app also modifies existing and inserts new records.
The TBytes of different records in the dataset often map to different fields representations, although setting a filter or range on the dataset will ensure that they have the same mapping.
As I said, extracting the known bytes and convert it into strings, dates, numbers and so on via calculated fields is no problem. Reconverting those values into the TBytes is also possible. The problem is making those extra fields editable, while keeping the dataset navigation intact.
If it helps: We have classes that do the bidirectional mapping, exposing the fields as published properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
答案取决于您正在使用的数据访问组件。我正在使用 Anydac,它支持 fkInternalCalc 字段,这些字段可以按照手动编辑的方式进行计算。
The answer depends on a data access components you are using. I am using Anydac and it support fkInternalCalc fields, which may be as calculated as manually edited.
我认为计算字段根据定义是只读的,其值在客户端计算。
您想要的可能可以通过可更新的视图来实现。您已经可以使用计算字段定义视图 - 这些字段将在服务器上的 SQL 中计算 - 以及更新触发器,也可能是插入触发器 - 来执行反向计算。然后,您可以从客户端像表格一样透明地使用视图。
I think calculated fields are by definition read-only, with values calculated on the client.
What you want could probably be implemented by an updatable view. You could define the view with calculated fields already - these would be calculated in SQL on the server - and an update trigger, maybe an insert trigger, too - to perform the reverse calculation. Then from the client you could use the view transparently like a table.
我在 ClientDataSet 上遇到了类似的问题,我已经使用 SQL-Stmt 上的虚拟字段解决了这个问题,这样我就可以模拟数据库中的字段。
请参阅我的问题
I had similar Issue with a ClientDataSet, i have solved this issue using dummy fileds on the SQL-Stmt so i could simulate the Fields in the Database.
See my Question
您可以使用 TDatasetProvider.OnGetRecords (不记得这是否是正确的事件名称)并修改发送到 Clientdataset 的数据包。
当然,正如 TOndrej 所说,您必须在 ApplyUpdates 处理程序上处理它们。
You can use TDatasetProvider.OnGetRecords (doesn't remember if this is the correct name of the event) and modify the datapacket sent to the Clientdataset.
Of course, you'll have to deal with them on an ApplyUpdates handler, as TOndrej said.
Woll2Woll 的 infopower 组件(我刚刚测试了他们的 TwwDBEdit)允许做这样的事情。所以我认为任何阻碍你的都是在 TDBEdit 级别(或 TFieldDataLink 级别)。
我不知道 TwwDBEdit 到底有什么不同。 (我不确定许可协议是否允许我在这里发帖......)。
Woll2Woll's infopower components (I just tested their TwwDBEdit) allow to do such thing. So I would think whatever blocks you is at the TDBEdit level (or at the TFieldDataLink level).
What exactly differs in TwwDBEdit, I don't know. (And I'm not sure the license agreement would allow me to post here...).
在我们的数据库设计中,某些值是相对于另一列的百分比(下面称为
oMean
),而其他浮点值则存储为绝对值。我们的客户后来希望所有字段都有两个选项(相对和绝对),因此我们提出了从TFloatField
派生的以下类。它应该适用于所有 TDataSet 后代。要在没有包的情况下使用它,您必须在打开数据集之前在
FormCreate
中创建字段:当然,可以在
OnCalcFields
事件中设置其内容或由用户。In our database design, some values are percentages relative to another column (called
oMean
below), whereas other float values are stored as absolutes. Our customers later wanted both options (rel. and abs.) for all fields, so we came up with the following class derived fromTFloatField
. It should work for all TDataSet descendants.To use it without a package, you have to create the field in
FormCreate
, before the dataset is opened:And of course, its contents can be set either in the
OnCalcFields
event or by the user.使用带有“Select *, 0 as TempField from MyTable”的 TQuery 后代 (MyQuery)
它现在是一个 editabe 临时字段
Use a TQuery descendant (MyQuery) with 'Select *, 0 as TempField from MyTable'
It is now an editabe temporary field