public List <Inmueble> BuscarPorPropietario(int idPropietario) { List <Inmueble> result = new List <Inmueble>(); Inmueble inmueble = null; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = "SELECT Id, Direccion, Ambientes, Uso, Tipo, Disponible, Precio, Superficie, PropietarioId," + " p.Nombre, p.Apellido" + " FROM Inmuebles i INNER JOIN Propietarios p ON i.PropietarioId = p.Id" + " WHERE IdPropietario=@idPropietario"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@idPropietario", idPropietario); command.CommandType = CommandType.Text; connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { inmueble = new Inmueble { Id = reader.GetInt32(0), Direccion = reader.GetString(1), Ambientes = reader.GetInt32(2), Uso = reader.GetString(3), Tipo = reader.GetString(4), Disponible = reader.GetString(5), Precio = reader.GetDecimal(6), Superficie = reader.GetString(7), PropietarioId = reader.GetInt32(8), Propietario = new Propietario { Id = reader.GetInt32(8), Nombre = reader.GetString(9), Apellido = reader.GetString(10) } }; result.Add(inmueble); } connection.Close(); } } return(result); }
public List <Inmueble> ObtenerTodos() { List <Inmueble> res = new List <Inmueble>(); using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = "SELECT i.Id, Direccion, Ambientes, Uso, Tipo, Disponible, Superficie, Precio, PropietarioId," + " p.Nombre, p.Apellido" + " FROM Inmuebles i INNER JOIN Propietarios p ON i.PropietarioId = p.Id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { Inmueble entidad = new Inmueble { Id = reader.GetInt32(0), Direccion = reader.GetString(1), Ambientes = reader.GetInt32(2), Uso = reader.GetString(3), Tipo = reader.GetString(4), Disponible = reader.GetString(5), Superficie = reader.GetString(6), Precio = reader.GetDecimal(7), PropietarioId = reader.GetInt32(8), Propietario = new Propietario { Id = reader.GetInt32(8), Nombre = reader.GetString(9), Apellido = reader.GetString(10), } }; res.Add(entidad); } connection.Close(); } } return(res); }