A
Abhishek Srivastava
Hello All,
Suppose if I have a SQL query like
select p.ID, p.NAME, p.UNIT_PRICE, o.QUANTITY from PRODUCT p ORDERS o
where p.ID = X AND P.ID = O.ID
Here one product can have many orders. Essentially Product is the master
table and Orders is the details table.
When I execute this query and populate a DataSet object I get only one
table in the DataSet object.
But this not what is ideal. Ideally I would like to get two tables in
the DataSet one for Products and other for Orders.
I can get two tables is to execute two queries.
select id, name, unit_price from products where id=x
select quantity from orders, products where products.id = x and
orders.id = products.id
But this involves two sql queries.... so not good.
Second solution is to iterate thru the sql data reader and create a
datarows into handbuilt DataSet. if the product id is the same, then
just add another DataRow to the order table, if the product id changes
then, add another DataRow into product table and keep iterating for
orders for this product. But this solution is very error prone and also
it complicates the code.
Another solution is to execute SQL with FOR XML AUTO clause. which
returns XML which contains data in proper relationship. But XML is an
overkill for this kind of solution.
Is there any other solution to this problem none of the above solutions
are very good ones.
regards,
Abhishek.
Suppose if I have a SQL query like
select p.ID, p.NAME, p.UNIT_PRICE, o.QUANTITY from PRODUCT p ORDERS o
where p.ID = X AND P.ID = O.ID
Here one product can have many orders. Essentially Product is the master
table and Orders is the details table.
When I execute this query and populate a DataSet object I get only one
table in the DataSet object.
But this not what is ideal. Ideally I would like to get two tables in
the DataSet one for Products and other for Orders.
I can get two tables is to execute two queries.
select id, name, unit_price from products where id=x
select quantity from orders, products where products.id = x and
orders.id = products.id
But this involves two sql queries.... so not good.
Second solution is to iterate thru the sql data reader and create a
datarows into handbuilt DataSet. if the product id is the same, then
just add another DataRow to the order table, if the product id changes
then, add another DataRow into product table and keep iterating for
orders for this product. But this solution is very error prone and also
it complicates the code.
Another solution is to execute SQL with FOR XML AUTO clause. which
returns XML which contains data in proper relationship. But XML is an
overkill for this kind of solution.
Is there any other solution to this problem none of the above solutions
are very good ones.
regards,
Abhishek.