So I'm in total n00b mode here.

So I'm in total n00b mode here.

I'm trying to set up a master/detail relationship with two TClientDataSets, accessing two tables via dbExpress' TSQLQuery.

I'd love to use something else (like FireDAC) but got a severe time constraint here and this is extending existing code.

Anyway, anyone know of a clear guide for doing this? My Google-fu seems to lacking today.

I've tried adapting this[1] EDN article, but it's not working and it's not clear to me how it will figure out the correct master/detail relationships anyway (ie how does it know what field in the detail table is referencing the master etc).

[1]: http://edn.embarcadero.com/article/29825

Comments

  1. I've not looked at the article you reference in detail but suffice it to say there's basically 2 ways to do master-detail datasets in the client tier -- 1 is by having embedded detail dataset data embedded in each master record, that is effectively fetched via 1 master provider and "unpacked"/accessed on the client via client datasets in some sort of relationship in the client.  This seems to be what they're doing in that article.  So the answer to your question is in that case that the data is directly sourced from a dataset field on the master record.  

    The other way is to have separate providers for master and detail in the server, and then manage the master-detail relationship in the client yourself. This can be substantially more efficient on the back-end  and makes possible/easy various types of offline operations (for example searching/filtering) in the client which would be difficult using the other approach if you had to find something based on data effectively embedded in the detail datasets in the master records.  Which better fits your use case will probably depend on various factors (briefcase app or not, speed of database, client side detail dataset functionality required. etc)

    If you don't get unstuck post back and I'll try to help if I can find some free time over the weekend.

    ReplyDelete
  2. This is not a briefcase-type app, the server is the db server. I'm strongly considering route two, just doing it manually, but it seems route 1 (nested) may save me some tedious work, hence why I was attempting that.

    So far I can't see how to wire things up correctly for the nested approach though. For example, the article I mentioned doesn't specify the query layer at all.

    ReplyDelete
  3. IIRC, TClientDataSet assumes equally named fields for this type of master-detail relations.

    ReplyDelete
  4. Uwe Raabe Ah bugger... I had a feeling, since I didn't see any mention of how to "explain" the relationship in the database. 

    Think I'll go the manual route for now then, thanks guys.

    ReplyDelete
  5. Fully agree with Walter's answer, but if you did want to link them up:

    Say you have two master/detail tables, Companies and Contacts, linked by a CompanyCode.

    Drop down two query components of your choice, eg. ADO. I'll assume a prefix of "qu"

    quCompanies.SQL should be set to "Select * from Companies"

    quContacts.SQL should be set to "Select * from Contacts where CompanyCode = :CompanyCode" (The param name should match the field name in Companies)

    Add a datasource, connect it to quCompanies.

    Set the datasource property of quContacts to that datasource as well.

    Drop down 1 TDataSetProvider component, link to quCompanies.

    Drop down 2 ClientDatasets - prefix of "cd".

    Link cdCompanies to the Provider, retrieve the fields for it. You'll see one of the fields is called quContacts.

    Set the datasetfield property of cdContacts to that quContacts field.

    That should be it I think.

    ReplyDelete
  6. Richard Stevens Ah, that explains the missing bits. Thanks!

    ReplyDelete

Post a Comment