Delphi7、dbExpress和Master Detail关系
我是德尔福新手。我通过两个 TDataSetProviders 链接到两个 TClientDataSets(分别为 cdsA 和 cdsB)的两个 TSQLTable(例如 A 和 B),两个 DataSource(dsA 和 dsB)完成了该场景。
设 A 为主,B 为从。
B.MasterSource 设置为 dsA 值,B.MasterFields 值引用 cdsA 中不存在(但存在于查询中)的字段。当我启动应用程序时,我首先打开 cdsA,然后打开 cdsB。出了问题。链接到 dsA 数据源的 DBGrid 显示数据,链接到 dsB 的 DBGrid 不显示任何内容。 SQLMonitor 日志文件显示执行了 B 中实现的查询(一个简单的 select a, b, c from tableB
)。如果我更改查询并显示字段“X”(从 tableB 中选择 a、b、c、X
),那么这些内容就可以正常工作,其中“X”是 B.IndexFieldNames 属性引用的字段。
为什么链接到 dsB 的 DBGrid 不显示与 cdsA 当前记录相关的 B 记录?仅当我在查询列中指定 IndexFieldNames
时它才有效吗?我错过了什么? TIA。
I'm new to Delphi. I got two TSQLTables (say A and B) linked to two TClientDataSets (say cdsA and cdsB respectively) by two TDataSetProviders, two DataSources (dsA and dsB) complete the scenario.
Let A be the master one and let B be the detail one.
B.MasterSource is set to dsA value and B.MasterFields value refer to a field that does not exists in cdsA (but exists in the query). When I start the application I open cdsA first and then I open cdsB. Something goes wrong. The DBGrid that link to dsA datasource shows data, the DBGrid that link to dsB does not show anything. The SQLMonitor logfile shows that the query implemented in B is executed (a simple select a, b, c from tableB
). The stuff works fine if I change the query and show the field 'X' (select a, b, c, X from tableB
) where 'X' is the field referred by B.IndexFieldNames property.
Why the DBGrid that link to dsB does not show the B's record related to cdsA's current record? Does it works only if I specify the IndexFieldNames
in the query columns? What did I miss? TIA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将解释使用 SQL Server 2008 R2 的 AdventureWorks 数据库的完整场景。我还假设您已经放置了
TSQLConnection
组件并正确设置其参数以建立与数据库的连接。对于此示例,我还将假设其名称为 Conn1。在表单上,放置 2 个
TSQLTable
(名为 tableA 和 tableB)、2 个TDataSetProvider
(名为 dspA 和 dspB),2TClientDataSet
(名为 cdsA 和 cdsB),2TDataSource
(名为dsA和dsB)和2个TDBGrid
(名为gridA和< strong>gridB) 组件。按如下方式设置属性:
在 gridA 中,您应该看到所有客户,而在 gridB 中,您应该只看到相关的订单给当前选择的客户。
这是在 Delphi 中两个 TClientDataSet 组件之间建立主/从关系的基本示例。然而,还有其他方法可以做到这一点。
I'll explain the complete scenario using AdventureWorks database for SQL Server 2008 R2. I'll also assume that you've already placed the
TSQLConnection
component and properly set its parameters to established connection with your database. For this example, I'll also assume the name for it to be Conn1.On a form, place 2
TSQLTable
(named tableA and tableB), 2TDataSetProvider
(named dspA and dspB), 2TClientDataSet
(named cdsA and cdsB), 2TDataSource
(named dsA and dsB) and 2TDBGrid
(named gridA and gridB) components.Set properties as follows:
In gridA you should see all Customers, and in gridB you should see only Orders related to curently selected customer.
This is the basic example of establishing master/detail relationship between two TClientDataSet components in Delphi. However, there are other ways to do this.
Cary Jensen 的书“Delphi In Depth:Client DataSets” 中概述了我链接 ClientDataSets 的方式。按照正常方式设置主数据集和详细数据集,并确保它们通过 TDataSource 链接(详细 SQL 中将有一个参数将其链接到主数据集)。然而,CJ 建议只使用一个附加到 Master 的 DataSetProvider。但是主设备(以及 DSP)将有一个嵌套数据集来重新发送详细信息表。详细信息/嵌套数据集可以出现在主表 DBGrid 或其自己的 DBGrid 中。您的 gridB 将链接到嵌套数据集。
将 gridB 直接链接回 TSQLQuery(据我所知)的问题是对主 CDS 的任何更新都不会反映在 gridB 中。如果您想了解更多信息,可以从 Cary 的网站下载项目 NestedFromMasterDetail。
如果你真的想了解更多,那就买一本卡里的书。我发现它对于理解客户数据集非常有价值。它们的设置有些不同,Cary 很好地解释了它们的架构。
The way that I link ClientDataSets is outlined in Cary Jensen's book "Delphi In Depth: Client DataSets". Setup the Master and Detail datasets as per normal, and ensure that they are linked via a TDataSource (you will have a parameter in the Detail SQL that links it to the Master). However, CJ suggests then having only one DataSetProvider which is attached to the Master. But the master (and therefore the DSP) will have a Nested DataSet reresenting the detail table. The detail / nested dataset can appear in the master table DBGrid or in its own DBGrid. Your gridB will get linked to the Nested Dataset.
The problem in linking gridB directly back to the TSQLQuery (as I understand) is that any updates to the master CDS are not reflected in gridB. If you want to see more then you can download the project NestedFromMasterDetail from Cary's web site.
If you really want to know more, then buy a copy of Cary's book. I have found it invaluable in understanding Client Data Sets. They are setup somewhat different and Cary does a good job of explaining their architecture.