public List <Inmueble> ObtenerPorPropietario(int id) { var res = new List <Inmueble>(); using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = "SELECT i.Id, Direccion, Tipo, Ambientes, Superficie, Importe, PropietarioId," + " p.Nombre, p.Apellido" + " FROM Inmuebles i INNER JOIN Propietarios p ON i.PropietarioId = p.Id WHERE i.Estado = 1 AND p.Estado = 1 AND p.Id = @id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; connection.Open(); command.Parameters.AddWithValue("@id", id); var reader = command.ExecuteReader(); while (reader.Read()) { Inmueble i = new Inmueble { Id = reader.GetInt32(0), Direccion = reader.GetString(1), Tipo = reader.GetInt32(2), Ambientes = reader.GetInt32(3), Superficie = reader.GetInt32(4), Importe = reader.GetInt32(5), PropietarioId = reader.GetInt32(6), Duenio = new Propietario { Id = reader.GetInt32(6), Nombre = reader.GetString(7), Apellido = reader.GetString(8), } }; res.Add(i); } connection.Close(); } } return(res); }
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 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()) { 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); }
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 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 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), Nombre = reader.GetString(7), Apellido = reader.GetString(8), } }; res.Add(i); } connection.Close(); } } return(res); }