Пример #1
0
        public Inmueble ObtenerPorId(int id)
        {
            Inmueble entidad = null;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string sql = $"SELECT i.Id, Direccion, Ambientes, Superficie, Latitud, Longitud, PropietarioId, p.Nombre, p.Apellido, Tipo, Uso, Precio, Estado" +
                             $" FROM Inmuebles i INNER JOIN Propietarios p ON i.PropietarioId = p.Id" +
                             $" WHERE i.Id=@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();
                    if (reader.Read())
                    {
                        entidad = new Inmueble
                        {
                            Id            = reader.GetInt32(0),
                            Direccion     = reader.GetString(1),
                            Ambientes     = reader.GetInt32(2),
                            Superficie    = reader.GetInt32(3),
                            Latitud       = reader.GetDecimal(4),
                            Longitud      = reader.GetDecimal(5),
                            PropietarioId = reader.GetInt32(6),
                            Tipo          = reader.GetString(9),
                            Uso           = reader.GetString(10),
                            Precio        = reader.GetDecimal(11),
                            Estado        = reader.GetInt32(12),
                            Duenio        = new Propietario
                            {
                                Id       = reader.GetInt32(6),
                                Nombre   = reader.GetString(7),
                                Apellido = reader.GetString(8),
                            }
                        };
                    }
                    connection.Close();
                }
            }
            return(entidad);
        }