One-To-Many Example

  • Thread starter Thread starter Stefan
  • Start date Start date
S

Stefan

I have been searching for this for a while but cannot seem to find what
I am looking for. Does anyone know where I can find a one-to-many
working example in c#? I have searched so many sites but no luck, I am
very confused in dealing with one DB and multiple tables. I just don't
know how to load the information from the various tables when I click
on records.

I have searched for step-by-step tutorials, but still nothing.

Appreciate any help on this as I am still learning all of this.


Thanks

Stefan


--
 
Stefan said:
I have been searching for this for a while but cannot seem to find what
I am looking for. Does anyone know where I can find a one-to-many
working example in c#? I have searched so many sites but no luck, I am
very confused in dealing with one DB and multiple tables. I just don't
know how to load the information from the various tables when I click
on records.

I have searched for step-by-step tutorials, but still nothing.

Appreciate any help on this as I am still learning all of this.


Thanks

Stefan

Assuming by 'one-to-many' you mean 'master-detail', maybe this will help:
http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx
 
BobF said:
Assuming by 'one-to-many' you mean 'master-detail', maybe this will
help: http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx


Hi Bob,

I think they are different, I could be wrong. Normally when I see a
master detail relationship it's only two tables.

My DB will have about 6 tables, and the data will be referenced by
either the date or a persons name from table one. The issue I am
having is the multiple rows of information that will be in various
listviews that has to be assigned to the specific date or name in table
one.

I have never done anything like this before, I have only worked with
two tables and that is pretty easy using non-DB controls. But this one
with 6 tables, I don't even know where to start.


--
 
I have been searching for this for a while but cannot seem to find what
I am looking for. Does anyone know where I can find a one-to-many
working example in c#? I have searched so many sites but no luck, I am
very confused in dealing with one DB and multiple tables. I just don't
know how to load the information from the various tables when I click
on records.

I have searched for step-by-step tutorials, but still nothing.

Appreciate any help on this as I am still learning all of this.


Thanks

Stefan

It sounds like your definition of one-to-many is different than I
commonly think. Take the following tables as an example:

Table 1: People: This table has peoples names, with a unique identifier.
Table 2: Cars: This table has a list of cars with a unique identifier.

Now, a person in table one can own more than one car, so there is a
one-to-many relationship that direction (Tiger Woods owns more than just
his Escalade). A car can also be owned by more than one person (the
Escalade is owned by Tiger Woods and his wife, for example). That also
is a one-to-many relationship.

Now the individual records to present to the user are "probably" a
single owner and a single car. So the above example may end up being
three or more rows presented to the user, such as:

Tiger Woods - Escalade
What's her name - Escalade
Tiger Woods - Golf Cart
etc...
 
Is it (dont really understand the problem) that you got a many to many
problem, in a conceptual model that is OK, but in a logical model that
breaks up to another table in the middle with one to many from brom
both tables.

like this: (using tiger and cars as the previus, but answer is to
Stefan)

tiger(person) owns (over time) many cars, key is person id
a car can (over time) be owned by many persons, key is car id

so a many to many between cars and persons is established, this has to
be broken down in the logical design of the database.

introduce a coupling table between persons and cars where the keys in
the couplig table is a combined key of person id and car id and
purchase date if you want to be able to sell and buy back that car ;)

//CY
 
It sounds like your definition of one-to-many is different than I commonly
think. Take the following tables as an example:

Table 1: People: This table has peoples names, with a unique identifier.
Table 2: Cars: This table has a list of cars with a unique identifier.

Now, a person in table one can own more than one car, so there is a
one-to-many relationship that direction (Tiger Woods owns more than just
his Escalade). A car can also be owned by more than one person (the
Escalade is owned by Tiger Woods and his wife, for example). That also is
a one-to-many relationship.

Actually, what you've described is a classic MANY-to-many relationship, and
there's usually a cross-reference table (or "intersection table," as my
co-worker says) involved. You usually only describe something as a
one-to-many relationship if it can ONLY go one way. Customers to orders is
one such classic example. A customer may have many orders with a store, but
a given order will belong to one and only one customer.
 
I have never done anything like this before, I have only worked with
two tables and that is pretty easy using non-DB controls. But this one
with 6 tables, I don't even know where to start.

You start by describing these six tables to us. We should be able to see how
they relate, if the design is sane....
 
Jeff said:
You start by describing these six tables to us. We should be able to
see how they relate, if the design is sane....


Here goes, I am going to try my best to explain what I am trying to do.

I will start off with three tables, once I get the concept of three I
know I can do four or more tables then.


Table1: Date, UserName, etc.
Table2: Date, Exercise, Sets, Notes, etc.
Table3: Date, Food, Type, Servings, etc.

Table1 will only have your standard fields from textboxes.

Table2 will have all the exercises performed on that date, everything
will be displayed in a listview. That could mean as much as 20 or more
rows of information in the listview.

Table3 will be similar to Table2, but it will just be nutrition for
that particular date thats selected.

So I have to connect Table1 date and UserName to the same date and
username in Table2 and Table3. I am assuming that my Unique key will
be the date. You can add more information for that specific day if
needed.

Did I explain it clearly enough? I have looked at some programs that
do exactly what I am trying to do, I had to download and install to
make sure.

To be honest, I think my confusion is in pulling the data to be
displayed. Lets say I select on the month calendar todays date.

12/10/2009

From Table1 I want to pull the information relation to that date and
display it in the fields on my form (simple enough).

On another part of my form, I also want the information from Table2
relating to that date to be displayed in a listview. Not simple for me
because its more than one row of information to be returned to fill the
listview.

Better thought, real example.

Table1:
12/8/2009, Steven Jones
12/9/2009, Steven Jones


Table2:
12/8/2009, Swimming, 4laps, good day
12/8/2009, Cardio, 30mins, tired
12/8/2009, Jog, 2laps, energy good
12/8/2009, Weights, 5sets, normal
12/9/2009, Yoga, 30mins, okay


Table3:
12/8/2009, Breakfast, Oatmeal, 2
12/8/2009, Break, Skake, 1
12/8/2009, Lunch, Full meal, 1
12/8/2009, Pre, carbs, 1
12/9/2009, Midday, Glucose, 2


Basically like what I put above. I can create the tables, I can create
the fields, I can pull the information from the field if its only one
row of information. If I have to pull the information from Table2 when
it references 12/8/2009, that is 4 rows of information to be pulled, I
don't know how to do more than one row.

--
 
Forgot to mention, my DB is going to be really simple. I will use
sqlite, and all the controls on the form will be non-db controls. I
don't want the program size to grow too big.

Thanks again everyone, I am learning...
 
Back
Top