1
votes

I have two tables whichs relation is expressed in a third n:m table. Lets call them Person, Workgroup and PersonWorkgroups. I have query which returns multiple Persons like "select all persons having a certain name". In addition I want to have all related Workgroups. At the end I would like to have a DataSet filled with the data in way that I can iterate over the customers and get their related Orders.

Has somebody a good example for the simplest way to get such a structure into a DataSet? I would prefere to do it in pure code without any typed recordsets, graphical editors, ...

cheers, Achim

3
Does it need to be in ado.net? I notice thats your tag - Raymund

3 Answers

0
votes

I would really suggest using LINQ to XML, just because I think it's an awesome way of working with a relational database.

If you just want to run the query and get a DataSet, can't you just make a union inside your query like this : select all personinfo, orderinfo from persons P, orders O where P.personno = O.orderno having a certain name?

Then you will get a DataSet with one line per order, and the person info linked to that order attached to each line.

0
votes

Not sure whether you mean DataTable or Dataset. If you execute the following query:

  select customer.customerid, customername, orderid, orderdate, orderamount
  from customer inner join orders
  on customer.customerid = orders.customerid and customer.customerid = {your id here passed as a parameter}

via a Command object, the results can be placed in a DataTable. If you execute the following two queries in one command, the result can be placed in a DataSet for client-side processing. You can define relations client-side using ADO.NET.

  select customerid, customername from customer;
  select orderid, orderdate, customerid, orderamount from orders

two tables are returned and you use DataAdapter.Fill() to populate the Dataset.

The DataTable approach could be "simpler" than the DataSet approach but it really depends on your application's requirements. Read up on the Command, DataAdapter, DataTable, and DataSet objects. With ADO.NET you can solve the problem in a variety of ways.

Since the question is also tagged TSQL: you could create a stored procedure and execute that via the ADO Command too:

     create proc getCustomerOrders
     @customerid int
     as
     begin
     select
     customer.customerid, customername, orderid, orderdate, orderamount
     from customer inner join orders
      on customer.customerid = orders.customerid and customer.customerid = @customerid
     end
0
votes

Tims answer is close to my requirement, but the relation between the tables has to be reconstructed in the C# code. In classic ADO it was possible to define such hierarchies in a special SQL dialect. Something like that is not possible with Ado.Net and T-SQL. So the answer to my question is: It's just not possible!