Delphi7、dbExpress和Master Detail关系

发布于 2024-12-17 07:14:06 字数 603 浏览 3 评论 0原文

我是德尔福新手。我通过两个 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 技术交流群。

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

发布评论

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

评论(2

土豪我们做朋友吧 2024-12-24 07:14:06

我将解释使用 SQL Server 2008 R2AdventureWorks 数据库的完整场景。我还假设您已经放置了 TSQLConnection 组件并正确设置其参数以建立与数据库的连接。对于此示例,我还将假设其名称为 Conn1

在表单上,​​放置 2 个 TSQLTable(名为 tableAtableB)、2 个 TDataSetProvider(名为 dspA dspB),2 TClientDataSet(名为 cdsAcdsB),2 TDataSource(名为dsAdsB)和2个TDBGrid(名为gridA和< strong>gridB) 组件。

按如下方式设置属性:

tableA.SQLConnection = Conn1
tableA.SchemaName = Sales
tableA.TableName = Customer
tableA.Active = True

dspA.DataSet = tableA

cdsA.ProviderName = dspA
cdsA.Active = True

dsA.DataSet = cdsA

gridA.DataSource = dsA

tableB.SQLConnection = Conn1
tableB.SchemaName = Sales
tableB.TableName = SalesOrderHeader
tableB.Active = True

dspB.DataSet = tableB

cdsB.ProviderName = dspB
cdsB.MasterSource = cdsA
cdsB.MasterFields = CustomerID
cdsB.Active = True

dsB.DataSet = cdsB

gridB.DataSource = dsB

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), 2 TDataSetProvider (named dspA and dspB), 2 TClientDataSet (named cdsA and cdsB), 2 TDataSource (named dsA and dsB) and 2 TDBGrid (named gridA and gridB) components.

Set properties as follows:

tableA.SQLConnection = Conn1
tableA.SchemaName = Sales
tableA.TableName = Customer
tableA.Active = True

dspA.DataSet = tableA

cdsA.ProviderName = dspA
cdsA.Active = True

dsA.DataSet = cdsA

gridA.DataSource = dsA

tableB.SQLConnection = Conn1
tableB.SchemaName = Sales
tableB.TableName = SalesOrderHeader
tableB.Active = True

dspB.DataSet = tableB

cdsB.ProviderName = dspB
cdsB.MasterSource = cdsA
cdsB.MasterFields = CustomerID
cdsB.Active = True

dsB.DataSet = cdsB

gridB.DataSource = dsB

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.

棒棒糖 2024-12-24 07:14:06

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.

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