How to show data from two tables in c#?

Asked

Viewed 191 times

3

I built a webservice in c#. With a table it was easy to display but what if I have two tables how would it look for me to display this data? I did so it’s the easiest way:

 [WebMethod]
     public String Teste()
     {
       SqlConnection Con = null;

       Mulheres_Cont contato= null;
       List<Object> contatos = new List<Object>();
       Con = new SqlConnection(WebConfigurationManager.ConnectionStrings["treinamentoBase"].ConnectionString);
       Con.Open();
       String Select = @"select tb_contato.nome, tb_contato.senha, tb_contato.usuario, tb_mulheres.nome, tb_mulheres.formacao from tb_contato inner join tb_mulheres on tb_contato.id = tb_mulheres.id";
       SqlCommand Cmd = new SqlCommand(Select, Con);
       SqlDataReader Dr = Cmd.ExecuteReader();
       while(Dr.Read())
       {

           contato = new Mulheres_Cont();

           contato.nome = Dr.GetString(0);
           contato.senha = Dr.GetString(1);
           contato.usuario = Dr.GetString(2);
           contato.nomeM = Dr.GetString(3);
           contato.formacao = Dr.GetString(4);

           contatos.Add(contato);

       }
       Con.Close();
       JavaScriptSerializer json = new JavaScriptSerializer();

       return json.Serialize(contatos);
    }

I think there’s another more elegant way to display this someone could inform me?

Type I created the table only to represent this data to be displayed in json format:

public class Mulheres_Cont
{
    public String nome { get; set; }
    public String senha { get; set; }
    public String usuario { get; set; }
    public String nomeM { get; set; }
    public String formacao { get; set; }
}

Would you have a more elegant way of doing that? I know I could create a class Woman :

   public class Mulher
   {
    public int id { get; set; }
    public Contato idContato { get; set; }
    public String nome { get; set; }
    public String formacao { get; set; }

    }

And this class is from a 1 : N relationship or a contact can have many women.

But this way I could not make someone give me a light of how it does that way? With an aggregation?

  • you want to display an object mulher and within it a list of mulher_cont???

  • This way it’s working. type it is returning the data but I have created a class called Mulheres_cont which is a Join Inner with two tables that exist in the SQL SERVER database which are tb_contact and tb_women. And I created a class in c# that is Mulheres_cont only to display this data

  • Okay. It worked. But I think there’s another way to do it and I’d like to know how you do it.

  • I wanted to do otherwise example wanted to instantiate the class Woman and contact class. And do something like: public class Woman { public int id { get; set; } public Contact idContact { get; set; } public String name { get; set; } public String formation { get; set; } }

  • I still don’t understand @gonz how you want to display?

  • Can a contact have many women or can it be associated with many women? Is the class Woman who can have several contacts not? That is, the woman class has an Ilist<Contact> internally. I understood so, that’s right?

  • You want to return a list of "Contact", in which each contact has a list of "Woman"? By the way, strange these entities, is system for what?

  • A system just for me to learn even to display otherwise. How do I insert a foreign key into the Woman table? A Contact can have several Women. Pretend that contact is a man. A man can have several women

  • I include everyone in the list and send them back. Understand?

Show 4 more comments

1 answer

2


public class Contato
{
    public String nome { get; set; }
    public String senha { get; set; }
    public String usuario { get; set; }

    ///<summary>
    /// Contém uma lista de mulheres
    ///</summary>
    public List<Mulher> mulheres {get; set; }    
}

public class Mulher
{
    public int id { get; set; }
    public Contato idContato { get; set; }
    public String nome { get; set; }
    public String formacao { get; set; }

}


while(Dr.Read())
{

    contato = new Contato();

    contato.nome = Dr.GetString(0);
    contato.senha = Dr.GetString(1);
    contato.usuario = Dr.GetString(2);

    //Irá adicionar um novo objeto a lista de mulheres em contato
    contato.mulheres = new List<Mulher>();
    contato.mulheres.Add(new Mulher
    {
        nome = Dr.GetString(3),
        formacao = Dr.GetString(4) 
    });

    contatos.Add(contato);

}
  • I think that’s right but the consultation to show how it would look?

  • In that part: while(Dr.Read()) { contact = new Mulheres_cont(); contact.name = Dr.Getstring(0); contact.password = Dr.Getstring(1); contact.user = Dr.Getstring(2); contact.name = Dr.Getstring(3); contact.formacao = Dr.Getstring(4); contacts. Add(contact); } Con.Close(); What it would look like

  • Let me do it here...

  • I didn’t quite understand your question, but I believe that’s what you wanted, I changed the code.

  • gives error in this part: contact.Women = new Woman { name = Dr.Getstring(3), formation = Dr.Getstring(4) };

  • 1

    This way, will repeat the contact by the number of women that the same possesses.

  • Yes, I agree with you @Fbatista, but I need to first understand if this is what he wanted.

  • 1

    @Edvaldofarias from what I understand, wants to send a list of contacts, with a list of women in each, just did not understand the question of when he asks how to display, after all is a Ws... I would click on a general list with all fields and then with a query I would group by contact. Or, easier, do with Dapper.

  • 1

    sorry @gonz is wrong anyway. This is how it should be: contact.women = new List<Woman>(); and then contact.women.Add(new Woman { ... } );

  • Oh my Deeeeeus how hard!!!!

  • It gave me strength, my God.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.