Beispiel #1
0
        public Inmueble ObtenerInmueble(int id)
        {
            var inmueble = new Inmueble();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string sql = $"SELECT i.Id, i.Direccion, Ambientes, Superficie, Latitud, Longitud, PropietarioId, p.Nombre, p.Apellido " +
                             $" FROM Inmuebles i INNER JOIN Propietario 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())
                    {
                        inmueble = 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),

                            propietario = new Propietario
                            {
                                Id       = reader.GetInt32(0),
                                Nombre   = reader.GetString(7),
                                Apellido = reader.GetString(8)
                            }
                        };
                    }
                    connection.Close();
                }
            }
            return(inmueble);
        }
Beispiel #2
0
        public List <Inmueble> ObtenerTodos()
        {
            var res = new List <Inmueble>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string sql = "SELECT i.Id, i.Direccion, Ambientes, Superficie, Latitud, Longitud, PropietarioId," +
                             " p.Nombre, p.Apellido" +
                             " FROM Inmuebles i INNER JOIN Propietario 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 i = 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),

                            propietario = new Propietario
                            {
                                Id       = reader.GetInt32(6),
                                Apellido = reader.GetString(7),
                                Nombre   = reader.GetString(8),
                            }
                        };
                        res.Add(i);
                    }
                    connection.Close();
                }
            }
            return(res);
        }