Question

By the few months I was trying to do an application using Sql Server + C#. The program is a kind of "Pokémon Agenda", where a user can add and edit pokémons and their abilities, types and photo, besides other functions like searching for records. Here's a print screen from the Form and from the tables diagram:

enter image description here

enter image description here

Here's my problem: I was trying to access directly the names from the tables "Abilities" and "Types" by only using its ID's (AbilityID and TypeID) from the table "Kanto". In other words, I was trying to access many data from different tables using, basically, just one table! However, I am not sure if this is exactly possible. I thought in 2 possibilities:

  1. Using Dataset relations (although this solution is not too clear for me yet), for example:

    ds.Relations.Add("PokéAbility", ds.Tables["Abilities"].Columns["AbilityID"], ds.Tables["Kanto"].Columns["AbilityID"]);

  2. Using a Natural Join, so I could create more tables and use them too to manipulate my desired data.

Note: the tables "Types" and "Abilities" should be already filled with all the necessary information, so they won't be edited during the execution of the Form.

I won't paste the whole code because it is a little long, but, if you would like to see a specific part of it, just ask me that I will provide it.

Was it helpful?

Solution

This should create a consolidated view. I've not tested the SQL so there might be a typo. This should get you started. You need to understand views and also joins.

Create View Pokedata as
    Select name_,  t1.type as Type1, t2.type as Type2, ability
    From Kanto 
    inner Join Abilities on Kanto.abilityId = Abilities.abilityId
    inner join KantoType as kt1 on Kanto.pokémonid = kt1.pokémonid and kanto.typeid1 = kt1.typeid
    inner join on types as t1 on kt1.typeid = t1.typeid
    inner join KantoType as kt2 on Kanto.pokémonid = kt2.pokémonid and kanto.typeid2 = kt2.typeid
    inner join on types as t2 on kt2.typeid = t2.typeid

OTHER TIPS

This isn't really going to anwer your question, because its a bit too vague to give any concrete advice, but I would suggest you have a look at the ADO.NET Entity Framework. Using Entity Framework, you can have classes that contain objects from other tables. For example (I'm guessing your data strunctures here...)

public class Pokemon
{
int Id {get; set;}
ICollection<PokeAbility> Abilities {get; set;}
ICollection<PokeType> Types {get; set;}
}

In Entity framework, you define a data context class, like this:

  public PokeContext : DbContext
    {
    DbSet<Pokemon> Kanto {get;set;}
    DbSet<PokeAbility> Abilies {get; set;}
    DbSet<PokeType> Types {get; set;}
    }

In your program then, once you have a Pokemon object, you can just access the data from the other tables like so:

var context = new PokeContext();
Pokemon instance = context.Kanto.First(); // Fetch first item from the Kanto table in the database
foreach (var ability in instance.Abilities)
  {
  Console.WriteLine(ability);  // Or whatever you want to do with it.
  }

I would suggest you have a look at some of the streaming videos that Microsoft has released with Visual Studio 2012, for example, this one:
http://msdn.microsoft.com/en-us/data/jj193542
There is a whole bunch of resources here:
http://msdn.microsoft.com/en-us/data/aa937723

Take a look at using SQL Server Views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top