public IList <Inmueble> BuscarPorPropietario(int id) { List <Inmueble> res = new List <Inmueble>(); Inmueble entidad = null; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = $"SELECT IdInmueble, Direccion, Uso, Tipo, Ambientes, Precio, Estado, i.IdPropietario, p.Nombre, p.Apellido" + $" FROM Inmueble i INNER JOIN Propietario p ON i.IdPropietario = p.IdPropietario" + $" WHERE p.IdPropietario=@id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.Add("@id", SqlDbType.Int).Value = id; command.CommandType = CommandType.Text; connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { entidad = new Inmueble { IdInmueble = reader.GetInt32(0), Direccion = reader.GetString(1), Uso = reader.GetString(2), Tipo = reader.GetString(3), Ambientes = reader.GetInt32(4), Precio = reader.GetInt32(5), Estado = reader.GetInt32(6), IdPropietario = reader.GetInt32(7), Propietario = new Propietario { IdPropietario = reader.GetInt32(7), Nombre = reader.GetString(8), Apellido = reader.GetString(9), } }; res.Add(entidad); } connection.Close(); } } return(res); }
public IList <Inmueble> ObtenerTodos() { IList <Inmueble> res = new List <Inmueble>(); using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = $"SELECT IdInmueble, Direccion, Uso, Tipo, Ambientes, Precio, Estado, Inmueble.IdPropietario, Propietario.Nombre, Propietario.Apellido FROM Inmueble INNER JOIN Propietario ON Propietario.IdPropietario=Inmueble.IdPropietario"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { Inmueble entidad = new Inmueble { IdInmueble = reader.GetInt32(0), Direccion = reader.GetString(1), Uso = reader.GetString(2), Tipo = reader.GetString(3), Ambientes = reader.GetInt32(4), Precio = reader.GetInt32(5), Estado = reader.GetInt32(6), IdPropietario = reader.GetInt32(7), Propietario = new Propietario { IdPropietario = reader.GetInt32(7), Nombre = reader.GetString(8), Apellido = reader.GetString(9) } }; res.Add(entidad); } connection.Close(); } } return(res); }