public IEnumerable<Projeto> MeusProjetos() { //precisaremos do id do usuario que esta logado.. int id = WebVoluntariacao.Controllers.UsuarioController.LoggedUser(); if (id == -1) return null; //caso o carinha nao esteja logado, o feed nao ira carregar... SqlConnection conn = new SqlConnection("Server=tcp:cohz2oez5j.database.windows.net,1433;Database=VOLUNTARIACAO;User ID=fhc@cohz2oez5j;Password=Volun123;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"); conn.Open(); SqlCommand cmd; SqlDataReader reader; List<Projeto> lista = new List<Projeto>(); cmd = new SqlCommand("SELECT P.ID_PROJETO, P.DESCRICAO_PROJETO, P.PONTUACAO, P.ENDERECO, P.CEP, P.BAIRRO, P.CIDADE, P.ESTADO, P.PAIS, P.NUM_END, P.NOME_PROJETO, P.DT_INICIO, P.DT_FINAL FROM TB_PROJETO P JOIN TB_USUARIO_PROJETO U ON P.ID_PROJETO=U.ID_PROJETO WHERE U.ID_USUARIO=@a and P.ATIVO=1", conn); cmd.Parameters.Add(new SqlParameter("@a", SqlDbType.VarChar)); cmd.Parameters[0].Value = id; reader = cmd.ExecuteReader(); //Obtém os registros, um por vez while (reader.Read()) { Projeto t = new Projeto(); t.id_projeto = reader.GetInt32(0); t.descricao_projeto = reader.GetString(1); t.pontuacao = reader.GetInt32(2); t.endereco = reader.GetString(3); t.cep = reader.GetString(4); t.bairro = reader.GetString(5); t.cidade = reader.GetString(6); t.estado = reader.GetString(7); t.pais = reader.GetString(8); t.num_end = reader.GetString(9); t.nome_projeto = reader.GetString(10); t.dt_inicio = reader.GetDateTime(11); t.dt_final = reader.GetDateTime(12); lista.Add(t); } reader.Close(); reader.Dispose(); cmd.Dispose(); conn.Close(); conn.Dispose(); return lista; }
public IEnumerable<Projeto> Listar() { int id_usuario = UsuarioController.LoggedUser(); //pegamos o id do usuario.. SqlConnection conn = new SqlConnection("Server=tcp:cohz2oez5j.database.windows.net,1433;Database=VOLUNTARIACAO;User ID=fhc@cohz2oez5j;Password=Volun123;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"); conn.Open(); SqlConnection q = new SqlConnection("Server=tcp:cohz2oez5j.database.windows.net,1433;Database=VOLUNTARIACAO;User ID=fhc@cohz2oez5j;Password=Volun123;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"); q.Open(); SqlCommand cmd; SqlCommand cmd2; SqlDataReader reader; List<Projeto> lista = new List<Projeto>(); cmd = new SqlCommand("SELECT ID_PROJETO, DESCRICAO_PROJETO, PONTUACAO, ENDERECO, CEP, BAIRRO, CIDADE, ESTADO, PAIS, NUM_END, NOME_PROJETO, DT_INICIO, DT_FINAL FROM TB_PROJETO WHERE ATIVO=1", conn); reader = cmd.ExecuteReader(); //Obtém os registros, um por vez while (reader.Read()) { Projeto t = new Projeto(); t.id_projeto = reader.GetInt32(0); t.descricao_projeto = reader.GetString(1); t.pontuacao = reader.GetInt32(2); t.endereco = reader.GetString(3); t.cep = reader.GetString(4); t.bairro = reader.GetString(5); t.cidade = reader.GetString(6); t.estado = reader.GetString(7); t.pais = reader.GetString(8); t.num_end = reader.GetString(9); t.nome_projeto = reader.GetString(10); t.dt_inicio = reader.GetDateTime(11); t.dt_final = reader.GetDateTime(12); //Agora precisamos passar se este usuario ja faz parte ou não deste tal projeto... cmd2 = new SqlCommand("SELECT COUNT(id_usuario) FROM TB_USUARIO_PROJETO WHERE id_usuario=@a AND id_projeto=@b", q); cmd2.Parameters.AddWithValue("@a", id_usuario); cmd2.Parameters.AddWithValue("@b", t.id_projeto); t.ismember = (int)cmd2.ExecuteScalar(); //vai passar 0 se nao for membro e 1 se for membro!! cmd2.Dispose(); lista.Add(t); } q.Close(); q.Dispose(); reader.Close(); reader.Dispose(); cmd.Dispose(); conn.Close(); conn.Dispose(); return lista; }