如何从属性必需设置为 false 的字段中检索信息?

发布于 2024-12-29 12:38:12 字数 1130 浏览 5 评论 0原文

我的软件中有以下 DBX 结构:

TSQLDataSet -> TDataSetProvider -> TClientDataSet

我的 TClientDataSet 中的字段之一的“Required”属性设置为 false,因为该字段根据数据库 (Firebird) 上的触发器和生成器自动递增。

但是,在配置 TSQLDataSet 和 TClientDataSet 且不需要此字段后,当我尝试从 TClientDataSet 读取此字段时,我得到了非常奇怪的结果。我怀疑我可能需要做一些额外的事情来强制我的 TClientDataSet 在这种情况下获取该字段的值。

我在这里缺少什么?

提前致谢。

编辑

必需属性的帮助文件对此进行了说明,但我不太明白它要我做什么。

描述

指定字段的非空值是否为 必需的。

使用“Required”来查明某个字段是否需要值,或者该字段是否 可以为空。

如果使用字段编辑器创建字段,则设置此属性 基于基础表。将“必需”设置为 true 的应用程序 对于必须具有值的字段(例如,密码或部分 number),但基础表不需要 字段,必须编写 OnValidate 事件处理程序来强制执行该属性。

当必需属性反映基础属性时 数据库表,尝试后应用空值会导致异常 被提出。将Required属性设置为true的应用程序 基础表不需要该字段,应该提出一个 OnValidate 事件中空值的 EDatabaseError 异常 处理程序以达到相同的结果。

编辑2

忘记提及:在TDataSetProvider和TClientDataSet之间,有一个DataSnap层(TClientDataSet连接是通过DataSnap驱动程序建立的)。

编辑 3

我使用此 DataSnap 设置创建了一个小型测试用例,它运行得很好。该项目是遗留的、混乱的,我想要么是我在某处配置了一个晦涩的选项,要么是我无意中遇到了 DataSnap bug。

I have the following DBX structure in my software:

TSQLDataSet -> TDataSetProvider -> TClientDataSet

One of the fields from my TClientDataSet has the property Required set to false, because this field auto increments based on triggers and generators on the database (Firebird).

However, after configuring both TSQLDataSet and TClientDataSet with this field not being required, I'm getting really weird results when I try to read this field from my TClientDataSet. I suspect that I might need to do something extra to force my TClientDataSet to acquire the value of this field in this condition.

What am I missing here?

Thanks in advance.

EDIT

The help file for the Required property says something about this, but I couldn't quite understand what it want me to do.

Description

Specifies whether a nonblank value for a field is
required.

Use Required to find out if a field requires a value or if the field
can be blank.

If a field is created with the Fields editor, this property is set
based on the underlying table. Applications that set Required to true
for fields that must have values (for example, a password or part
number), but for which the underlying table does not require the
field, must write an OnValidate event handler to enforce the property.

When the Required property reflects a property of the underlying
database table, trying to post apply a null value causes an exception
to be raised. Applications that set the Required property to true when
the underlying table does not require the field, should raise an
EDatabaseError exception on null values in the OnValidate event
handler in order to achieve the same result.

EDIT 2

Forgot to mention: between the TDataSetProvider and the TClientDataSet, there is a DataSnap layer (the TClientDataSet connection is made with a DataSnap driver).

EDIT 3

I created a small test case with this DataSnap setup and it worked perfectly. The project is legacy, messy and I guess that either I have an obscure option configured somewhere that is biting me or I have stumbled in a DataSnap bug.

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

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

发布评论

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

评论(2

云仙小弟 2025-01-05 12:38:12

好乐,你插入后尝试过TClientDataset.RefreshRecord吗?或者甚至 TClientDataset.Refresh?
有了生成器,您甚至可以在像 select gen_id(generator,1) from RDB$Database 这样的查询中提前获取生成器(在调用 ApplyUpdates 之前)(这是从内存中获取的,这里没有 Firebird test)并提前填写PK字段。

编辑:似乎这是一个 heisenbug。我会尝试删除组件并从头开始再次重新配置它们(这意味着:删除后,保存并关闭 Delphi)。

或者更好的是,创建一个仅包含所需查询配置的空项目,并尝试在 TDBGrid 中查看该数据。如果这个问题仍然发生,可能你的FB安装有一些组件损坏(甚至Delphi安装)

Haole, have you tried TClientDataset.RefreshRecord after inserting? Or even TClientDataset.Refresh?
Having generators, you can even get the generator in advance (before calling ApplyUpdates) in a query like select gen_id(generator,1) from RDB$Database (it's from memory, don't have Firebird here to test) and fill the PK field in advance.

EDIT: seems this is a heisenbug. I would try to remove the components and reconfigure them again from scratch (which means: after you remove, save and close Delphi).

Or even better, create an empty project with just that needed query configuration and try to view that data in a TDBGrid. If this problem still happens, maybe your FB installation have some component corrupted (or even Delphi installation)

调妓 2025-01-05 12:38:12

似乎问题是一个过时的字段被读取为 INTEGER,并且它是数据库中的 SMALLINT。

这个问题很难调试,而且这个问题具有误导性。感谢所有帮助我调试此问题的人。

Seems that the problem was an outdated field being read as an INTEGER and it was a SMALLINT in the database.

This problem was hard to debug and this question was misleading. Thanks for everyone that helped me debug this.

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