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 ofmulher_cont
???– novic
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
– Aline
Okay. It worked. But I think there’s another way to do it and I’d like to know how you do it.
– Aline
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; } }
– Aline
I still don’t understand @gonz how you want to display?
– novic
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?
– rodrigorf
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?
– FBatista
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
– Aline
I include everyone in the list and send them back. Understand?
– Aline