public IList <Alquiler> ObtenerInmueblePorDni(string dni) { IList <Alquiler> res = new List <Alquiler>(); Alquiler a = null; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = $" SELECT AlquilerId, Descripcion, FechaAlta, FechaBaja, Monto, a.InmuebleId, a.InquilinoId," + $" iq.Nombre, iq.Apellido," + $" i.Direccion, i.Precio" + $" FROM Alquileres a join Inmuebles i ON a.InmuebleId = i.InmuebleId " + $" join Inquilinos iq ON a.InquilinoId = iq.inquilinoId" + $" WHERE iq.Dni = @dni"; using (var command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; command.Parameters.Add("@dni", SqlDbType.VarChar).Value = dni; connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { a = new Alquiler { AlquilerId = reader.GetInt32(0), Descripcion = reader.GetString(1), FechaAlta = reader.GetDateTime(2), FechaBaja = reader.GetDateTime(3), Monto = reader.GetString(4), InmuebleId = reader.GetInt32(5), InquilinoId = reader.GetInt32(6), Inquilino = new Inquilino { Nombre = reader.GetString(7), Apellido = reader.GetString(8), }, Inmueble = new Inmueble { Direccion = reader.GetString(9), Precio = reader.GetDecimal(10), }, }; res.Add(a); } connection.Close(); } } return(res); }
public Alquiler ObtenerPorId(int id) { Alquiler entidad = null; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = $"SELECT AlquilerId, Descripcion, FechaAlta, FechaBaja, Monto, al.InmuebleId, al.InquilinoId, " + $" inm.Direccion" + $" FROM Alquileres al, Inmuebles inm" + $" WHERE al.InmuebleId = inm.InmuebleId and " + $" al.AlquilerId=@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 Alquiler { AlquilerId = reader.GetInt32(0), Descripcion = reader.GetString(1), FechaAlta = reader.GetDateTime(2), FechaBaja = reader.GetDateTime(3), Monto = reader.GetString(4), InmuebleId = reader.GetInt32(5), InquilinoId = reader.GetInt32(6), Inmueble = new Inmueble { Direccion = reader.GetString(7), }, }; } connection.Close(); } } return(entidad); }