DataSet 1 to many Relation - How to?

I have two tables in the DataSet, the first present rooms and the other furniture kinds.

Furniture kind, like chare, can be in more then one room. And specific room can contain more then one furniture kind (chare + table + ...). Therefore, the room table should have Relation that point to several furniture kind, and the furniture table should have Relation to several rooms.

Is there a way to define this kind of relation from one to many?
If so - How?

Any advice will be highly appreciated.


---
Thanks
Sharon G.
[572 byte] By [sharong] at [2007-11-19 9:50:46]
# 1 Re: DataSet 1 to many Relation - How to?
You describe a Many-To-Many relation.
You will need to define two tables with primary key, and a relation table with a primary key consisting of two forign keys for the other tables.
I your case a ROOM table, a FURNITURE table, and a ROOM_FORNITURE table. Each raw in the latter table has one key from ROOM and one key from FURNITURE.
jhammer at 2007-11-9 1:48:37 >
# 2 Re: DataSet 1 to many Relation - How to?
Yes, it is possible to have relations in a dataset.

This is an example from MSDN Documentation for DataRelation class:

// Get the DataColumn objects from two DataTable objects in a DataSet.
DataColumn parentCol;
DataColumn childCol;
// Code to get the DataSet not shown here.
parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
childCol = DataSet1.Tables["Orders"].Columns["CustID"];
// Create DataRelation.
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
// Add the relation to the DataSet.
DataSet1.Relations.Add(relCustOrder);

You can also create a typed dataset (which I prefer), for the typed dataset relations can be created in a designer, just like in many database designers.

EDIT: and just like Jhammer pointed out, you are describing a many-to-many relation.
klintan at 2007-11-9 1:49:37 >
# 3 Re: DataSet 1 to many Relation - How to?
jhammer - I'm not sure I understand (I'm new in the DataSet area).

Can you give some more details/sample/post ?

Why do you say that I need many-to many relation and not one-to many?

Note: I'm designing a model using XSD file that the VS .NET generate a DataSet object from it.


---
Thanks
Sharon G.
sharong at 2007-11-9 1:50:41 >
# 4 Re: DataSet 1 to many Relation - How to?
Klintan - Yes, it is possible to define a relation, but I need one-to-many relation, and you describe one-to-one relation.

---
Thanks
Sharon G.
sharong at 2007-11-9 1:51:44 >
# 5 Re: DataSet 1 to many Relation - How to?
jhammer - I'm not sure I understand (I'm new in the DataSet area).

Can you give some more details/sample/post ?

Why do you say that I need many-to many relation and not one-to many?

Note: I'm designing a model using XSD file that the VS .NET generate a DataSet object from it.


---
Thanks
Sharon G.

Well, if you just open the xsd file in the designer, right click the table you want to create a relation for, select add - relation, define primary key if you not already you have done so, then you are done.

And I describe 1 to many relation. DataRelation are for one to many (parent-child).
klintan at 2007-11-9 1:52:45 >
# 6 Re: DataSet 1 to many Relation - How to?
A One-To-Many relation is a relation where A is related to a few instances of B, where B can be related only to one A.
For example a chair can be contained by one and only one room, where a room can contain several chairs. In this case you will have a ROOM table, a FURNITURE tabel, where each furniture row will have a foriegn key to the room. For example lets say we have a room with ID 5, then furnitures with ID 1,2,3 can have the Room field set to 5. the room will contain 3 furnitures and each furniture will be contained by one room.

However, this is not your case as you describe it. You say that each furniture can be contained by several rooms at a time. For example a chair can be contained by 2 rooms, where each room can contain other chairs as well. So you will need to do what i posted before (ROOM_FURNITURE table).

The way to do this using XSD, is to drag 3 elements from the ToolBox into the XSD designer area. Each one represent the table. Set the names of the elements (first rows) to ROOM, FURNITURE and ROOM_FURNITURE. Then set all fields. Make sure to have a primary key in both ROOM and FURNITURE table. And then drag relations the way klintan described. After that you can generate a typed DataSet with all 3 tables and all relations between them.
jhammer at 2007-11-9 1:53:39 >
# 7 Re: DataSet 1 to many Relation - How to?
Thanks a lot to you two for the input.

jhammer -
I think I'm missing something.
I already have the two tables ROOM and FURNITURE and each table contain one key RoomKey and FurnitureKey, and from what I understand, I need to add another element (table) to XSD diagram - that's fine.
But what this element should contain:
(1) How many elements?
(2) What type should I select for each element.
(3) What relation and how should I add? between which tables? I have 3 now.

Klintan - now that I know that the Relation is one-to-many/many-to-many, how do I set it to point from one room to several Furniture?

---
Thanks
Sharon G.
sharong at 2007-11-9 1:54:43 >
# 8 Re: DataSet 1 to many Relation - How to?
The ROOM_FURNITURE element should have only two fields: roomKey and FurnitureKey. Both of them should be of the same type as the keys from the other tables. They should both form a primary key together (you can do that with the designer). If you need more fields to describe the relation feel free to add those.

Add a Relation between the roomKey of table ROOM, and thr roomKey from table ROOM_FURNITURE. Same for furnitureKey (by draging the field from the ROOM table to the roomKey field in the ROOM_FURNITURE table).

Now, when you generate a typed dataset, lets say it is called MyDataSet, you will have a MyDataSet.ROOMRow, MyDataSet.FURNITURERow, and MyDataSet.ROOM_FURNITURERow. Each row of type ROOMRow will have a property ROOM_FURNITURE to describe the related rows from this table, and so on.
jhammer at 2007-11-9 1:55:43 >
# 9 Re: DataSet 1 to many Relation - How to?
DataRelation is one to many (not many to many).

Assume you have created a schema like in the attached file, code could look like this to define relations:

Dataset1 ds=new Dataset1();
Dataset1.FurnitureTypeRow chair=ds.FurnitureType.NewFurnitureTypeRow();
chair.FurnitureTypeId=1;
chair.Description="Chair";

Dataset1.FurnitureTypeRow table=ds.FurnitureType.NewFurnitureTypeRow();
table.FurnitureTypeId=2;
table.Description="Table";

Dataset1.RoomRow livingRoom=ds.Room.NewRoomRow();
livingRoom.RoomId=1;
livingRoom.Description="Living Room";

Dataset1.RoomRow kitchen=ds.Room.NewRoomRow();
kitchen.RoomId=2;
kitchen.Description="Kitchen";

Dataset1.FurnitureRow chairInKitchen=ds.Furniture.NewFurnitureRow();
chairInKitchen.FurnitureTypeId=chair.FurnitureTypeId;
chairInKitchen.RoomId=kitchen.RoomId;
chairInKitchen.DatePurchased=new DateTime(2005,06,09);

(Just to confuse things I have the FurnitureType for the type of furniture (e.g. Chair) and Furniture for the instance (e.g. the chair in the kitchen), where Jhammer has Furniture and Furniture_Room for the same thing.)

Datasets can be connected to database using data adapters, bound to controls, you can define auto-increment for the key columns so that you don't have to bother setting them, and do lots of other stuff that I not have shown here.
klintan at 2007-11-9 1:56:43 >
# 10 Re: DataSet 1 to many Relation - How to?
jhammer - I think I finally understand.

Assuming I do understand, now when I wish to find the relevant room for lets say a specific chare in the FURNITURE table, I first need to follow the relation from the specific FURNITURE row in the FURNITURE table to the ROOM_FURNITURE row in the ROOM_FURNITURE table, then to move to the sibling column, and from there to follow the relation to the relevant Room row at the ROOM table.

And I should do the same to find the corresponding furniture of a room.

Am I correct ?

I must say it's not that easy to navigate among the ROOM and FURNITURE table.

Isn't there an easier way to it?

Klintan - Can you show me how to navigate through the DataSet you beautifully described?
Lets say; how to navigate and find the room for a specific chair?


---
Thanks
Sharo G.
sharong at 2007-11-9 1:57:50 >
# 11 Re: DataSet 1 to many Relation - How to?
Here is an example of navigation:

// Loop through all furniture types
foreach (Dataset1.FurnitureTypeRow furnitureType in ds.FurnitureType)
{
Console.Write("Rooms that have a {0}: ",furnitureType.Description);
foreach (Dataset1.FurnitureRow furniture in furnitureType.GetFurnitureRows())
{
Console.Write("{0}, ",furniture.RoomRow.Description);
}
Console.WriteLine();
}
klintan at 2007-11-9 1:58:48 >
# 12 Re: DataSet 1 to many Relation - How to?
Klintan - Thanks for the sample.

Surprisingly for me it works.

Thanks a lot
Sharon G.
sharong at 2007-11-9 1:59:46 >
# 13 Re: DataSet 1 to many Relation - How to?
I am sorry to say that Many-To-Many relations are complicated. There is no easier way to navigate (at least I don't know of one).
jhammer at 2007-11-9 2:00:50 >
# 14 Re: DataSet 1 to many Relation - How to?
After looking further to the solution you have suggested, I'm puzzled with two unclear questions:
(1) If I have 3 rooms and 12 furniture all together, 4 furniture in each room. How many ROOM_FURNITURE tables will be created in this case that will hold the bi-directional relations between the rooms and the furniture (relations that point from furniture to room and from room to furniture) ?
How the ROOM_FURNITURE will look like?

(2) Lets say I'm trying to find the furniture that room 3 contains. So, I go from room 3 row through its key to the ROOM_FURNITURE row in the ROOM_FURNITURE table, but now I need to find to which furniture to go to because there are many furniture in that room or even many furniture for all the other rooms.


---
Thanks
Sharon G.
sharong at 2007-11-9 2:01:56 >
# 15 Re: DataSet 1 to many Relation - How to?
1) You will have 12 rows in ROOM_FURNITURE, as each row represents a concrete furniture (e.g. the green sofa in the living room), while the FURNITURE table contains furniture types (e.g. sofa)

2) When you go from room 3 row to its key in the ROOM_FURNITURE table, you find only the ROOM_FURNITURE rows for the furniture in room 3 (which can be several, but it cannot be furniture in other room per definition).
klintan at 2007-11-9 2:02:57 >
# 16 Re: DataSet 1 to many Relation - How to?
First you posted:
"Furniture kind, like chare, can be in more then one room"
now you post:
"I have 3 rooms and 12 furniture all together, 4 furniture in each room"

if you only have one room for each furniture (a furniture can't be in more than one room) than this is a one-to-Many relationship. yOu need to decide which one you need. a ONe-To-Many relationship is much simpler than a many-to-many relationship.

About your questions:
1. Only one ROOM_fURnITure table exist. one row for each room-furniture relation.

2. When you do a select method of the room-furniture table you can get all the rows regarding a curtein room/furniture. the select method of a table returns an array of datarow which you can cast to the room/furniture rows, and iterate through it using a for...each statement.
jhammer at 2007-11-9 2:03:58 >
# 17 Re: DataSet 1 to many Relation - How to?
I saying the same thing all the way:
I have a table that hold furniture types/kind (chair, table, closet etc.), and a table of rooms (room 1, room 2, ...). Any room can can contain any kind of furniture, meaning that room 2 and 3 may hold chairs so a chair is present in room 2 and 3.

I also posted an example for 12 kind of furniture, that are located in 3 separate rooms so each room contain 4 kind of furniture.

Any way, thanks a lot for the help. I finally understand this issue, I'm simple new at the DB area.


-----
Thanks again
Sharon G.
sharong at 2007-11-9 2:04:58 >