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