public Inmueble ObtenerPorId(int id) { Inmueble inmueble = null; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = $"SELECT inmueble.id, Direccion, Ambientes, Superficie, Latitud, Longitud, Precio, PropietarioId, Foto, " + "propietario.Nombre, propietario.Apellido " + $"FROM Inmuebles inmueble INNER JOIN Propietarios propietario ON inmueble.PropietarioId = propietario.id " + $"WHERE inmueble.id = @id AND inmueble.Estado = 1"; 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), Precio = reader.GetDecimal(6), PropietarioId = reader.GetInt32(7), Foto = reader.GetString(8), Propietario = new Propietario { Id = reader.GetInt32(7), Nombre = reader.GetString(9), Apellido = reader.GetString(10), } }; } connection.Close(); } } return(inmueble); }
public int Alta(Inmueble inmueble) { int res = -1; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = $"INSERT INTO Inmuebles (Direccion, Ambientes, Superficie, Latitud, Longitud, Precio, PropietarioId, Foto) " + $"VALUES (@direccion, @ambientes, @superficie, @latitud, @longitud, @precio, @propietarioId, @Foto); " + "SELECT SCOPE_IDENTITY();"; // devuelve el id insertado (LAST_INSERT_ID para mysql) using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@direccion", inmueble.Direccion); command.Parameters.AddWithValue("@ambientes", inmueble.Ambientes); command.Parameters.AddWithValue("@superficie", inmueble.Superficie); command.Parameters.AddWithValue("@latitud", inmueble.Longitud); command.Parameters.AddWithValue("@longitud", inmueble.Longitud); command.Parameters.AddWithValue("@precio", inmueble.Precio); command.Parameters.AddWithValue("@propietarioId", inmueble.PropietarioId); if (String.IsNullOrEmpty(inmueble.Foto)) { command.Parameters.AddWithValue("@Foto", ""); } else { command.Parameters.AddWithValue("@Foto", inmueble.Foto); } connection.Open(); res = Convert.ToInt32(command.ExecuteScalar()); // devuelve la primer columna de la primer fila de resultados del query (id) inmueble.Id = res; connection.Close(); } } return(res); }
public int Modificacion(Inmueble inmueble) { int res = -1; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = $"UPDATE Inmuebles SET Direccion = @direccion, Ambientes = @ambientes, " + $"superficie = @superficie, Latitud = @latitud, Longitud = @longitud, Precio = @precio, " + $"PropietarioId = @propietarioId, Foto = @Foto " + $"WHERE id = @id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@id", inmueble.Id); command.Parameters.AddWithValue("@direccion", inmueble.Direccion); command.Parameters.AddWithValue("@ambientes", inmueble.Ambientes); command.Parameters.AddWithValue("@superficie", inmueble.Superficie); command.Parameters.AddWithValue("@latitud", inmueble.Latitud); command.Parameters.AddWithValue("@longitud", inmueble.Longitud); command.Parameters.AddWithValue("@precio", inmueble.Precio); command.Parameters.AddWithValue("@propietarioId", inmueble.PropietarioId); if (String.IsNullOrEmpty(inmueble.Foto)) { command.Parameters.AddWithValue("@Foto", ""); } else { command.Parameters.AddWithValue("@Foto", inmueble.Foto); } connection.Open(); res = command.ExecuteNonQuery(); connection.Close(); } } return(res); }