I built a webservice in C #. With a table it was easy to display but what if I have two tables how would I display those data? I did so that'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 is another, more elegant way to display it, could anyone tell me?
Type I created the table just 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 this? I know I could create a Woman class:
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 ie a contact can have many women.
But this way I could not make someone give me a light of how it does this way? With an aggregation?