Question

Is it possible, using an ADO.NET typed DataSet containing two tables in a parent/child relationship, to populate the DataSet with ONE trip to the d/b (query could return one or two tables; if one, then result set has columns from both tables, right?), and to update the d/b with ONE trip to the d/b (call to generated stored proc, I guess).

By "is it possible", I mean is it possible to have Visual Studio (2012) automagically generate the classes and SQL code to make this happen?

Or am I kind of on my own? It's looking an awful lot like VS really wants to generate one d/b server round trip for each table involved.

*I guess the update stored proc would have to take table-typed parameters from both parent and child, and perform inserts/updates/deletes appropriately.

Was it helpful?

Solution

Yes, one round trip per table is the way to go. (- It's certainly possible to use a join query to populate a datatable but VS will then be reluctant to generate update etc SQL. This may or may not be a problem, depending on what you intend to do with the dataset.) But if you have two tables in a dataset, lets say customers - orders, then you would typically use two queries, and two trips to the db:

SELECT * FROM customers WHERE customers.customerid=@customerid

and SELECT * FROM orders WHERE orders.customerid=@customerid

Somewhat more counter-intuitive is the situation where you want all customers and orders for one country:

SELECT * FROM customers WHERE customers.countryid=@countryid

and

SELECT orders.* FROM orders INNER JOIN customers ON customers.customerid=orders.customerid WHERE customers.countryid=@countryid

Note how the join query returns data from only one table, but uses the join to identify which rows to return.
Then, once you have the data in your dataset, you can navigate it using the getparentrow and getchildrows methods. This is how ADO.Net manages hierarchical data. You do need this one-table-at-a-time approach, because, assuming you have foreign key constraints in your db, you need to insert and update in reverse order from delete.

EDIT Yes, this does mean that in some circumstances, depending on the data you want and the structure of your primary keys, you could end up with a humungous set of JOINS that still only pull the data from the table at the end of the hierarchy. This might seem wrong in terms of traditional SQL, but actually it's fine. The time you have lost in the multiple, more complex queries is saved by the reduced amount of data you have to pull back across the wire, compared with one big join query that would be returning multiple copies of the parent data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top