SeToY
SeToY

Reputation: 5895

Separating data the right way

Let's imagine the following scenario:

I've got a table "Types" and a table "Things". Each "thing" has a type defined via the "Things.TypeId" column, which has a foreign key on the "Types.Id" column.

Now I want to access these Types via my C#-Code, that loads the types into a List so I can use it in my application.

Now let's think of a method, that tries to load all "Things" that have the type "one" to my application.

SELECT * FROM Things WHERE TypeId = {0}"

Where do I now get the TypeId for the Type "one"? Normally, enums would be suitable for that, but we can't use enums since the Types are stored in my Database and retrieved on runtime.

How can I deal with this problem?

Upvotes: 0

Views: 63

Answers (1)

paparazzo
paparazzo

Reputation: 45096

You can do it with a join

select things.* 
from things 
join type on thing.typeID = type.typeID 
where type.name = 'one'

Or when the program starts you could load type into a Dictionary with typeID the key and name the value. You could also bind that DictionaryList to Combobox where you display the value but use the key.

Upvotes: 2

Related Questions