public Boolean AddFlightRecord(Flight flight) { if (!IsConnected) throw new NotConnectedException(); try { IPilotCheckin db = CreateChannel(_factory); return db.AddFlightRecord(flight) == ReturnCode.Success; } catch (Exception e) { Console.WriteLine(e.Message); throw; } }
/// <summary> /// Returns a list of all flights past and future /// </summary> /// <returns></returns> public List<Flight> GetFlights() { List<Flight> flights = new List<Flight>(); try { using (SqlCeConnection dbConnection = new SqlCeConnection( "Data Source=\"SKDDatabase.sdf\"")) { dbConnection.Open(); SqlCeCommand dbCommand = new SqlCeCommand(@"SELECT * FROM flights", dbConnection); SqlCeDataReader dbReader = dbCommand.ExecuteReader(); while (dbReader.Read()) { Flight flight = new Flight { FlightNumber = dbReader.IsDBNull(0) ? (int?) null : dbReader.GetInt32(0), FamilyId = dbReader.IsDBNull(1) ? (int?) null : dbReader.GetInt32(1), CraftId = dbReader.IsDBNull(2) ? null : dbReader.GetString(2), DepartureTime = dbReader.IsDBNull(3) ? (DateTime?) null : dbReader.GetDateTime(3), ArrivalTime = dbReader.IsDBNull(4) ? (DateTime?) null : dbReader.GetDateTime(4), MaxWeight = dbReader.IsDBNull(5) ? (double?) null : dbReader.GetDouble(5), NumberOfSeats = dbReader.IsDBNull(6) ? (int?) null : dbReader.GetInt32(6), CheckInTime = dbReader.IsDBNull(7) ? (DateTime?) null : dbReader.GetDateTime(7), Guid = dbReader.IsDBNull(8) ? (Guid?) null : dbReader.GetGuid(8), Notes = dbReader.IsDBNull(9) ? null : dbReader.GetString(9) }; flights.Add(flight); } } } catch (Exception e) { Console.WriteLine("Unexpected error: " + e.Message); } return flights; }
/// <summary> /// Creates a new flight record for a pilot that is checking in. /// </summary> /// <param name="flight"></param> /// <returns></returns> public ReturnCode AddFlightRecord(Flight flight) { ReturnCode returnCode = ReturnCode.Success; try { using (SqlCeConnection dbConnection = new SqlCeConnection( "Data Source=\"SKDDatabase.sdf\"")) { dbConnection.Open(); SqlCeParameter craftID = new SqlCeParameter("@craftIDVar", SqlDbType.NVarChar) { Value = flight.CraftId }; SqlCeParameter numberOfSeats = new SqlCeParameter("@numSeatsVar", SqlDbType.Int) { Value = flight.NumberOfSeats }; SqlCeParameter maxWeight = new SqlCeParameter("@maxWeightVar", SqlDbType.Float) { Value = flight.MaxWeight }; SqlCeParameter checkInTime = new SqlCeParameter("@checkInTimeVar", SqlDbType.DateTime) { Value = DateTime.Now }; SqlCeParameter notes = new SqlCeParameter("@notesVar", SqlDbType.NVarChar); if (flight.Notes == null) { notes.Value = DBNull.Value; } else { notes.Value = flight.Notes; } SqlCeParameter guid = new SqlCeParameter("@guidVar", SqlDbType.UniqueIdentifier) { Value = Guid.NewGuid() }; SqlCeCommand checkinCommand = new SqlCeCommand(); checkinCommand.Parameters.Add(craftID); checkinCommand.Parameters.Add(numberOfSeats); checkinCommand.Parameters.Add(maxWeight); checkinCommand.Parameters.Add(checkInTime); checkinCommand.Parameters.Add(notes); checkinCommand.Parameters.Add(guid); checkinCommand.CommandText = @"INSERT INTO flights (craft_id, num_seats, max_weight, pcheck_in_time, guid, notes) VALUES (@craftIDVar, @numSeatsVar, @maxWeightVar, @checkInTimeVar, @guidVar, @notesVar)"; checkinCommand.Connection = dbConnection; checkinCommand.ExecuteNonQuery(); dbConnection.Close(); } } catch (Exception e) { Console.WriteLine("Unexpected error in PilotCheckinHandler: " + e.Message); returnCode = ReturnCode.Fail; } return returnCode; }
private void PilotSubmit() { if (SelectedPilot == null) return; Flight flight = new Flight { CraftId = SelectedPilot.CraftId, MaxWeight = FlightMaxWeight, NumberOfSeats = NumSeats, AssignedPilot = SelectedPilot }; if (_parent.PilotDatabase.AddFlightRecord(flight)) { Pilots.Clear(); Pilots.AddRange(_parent.PilotDatabase.GetPilots()); NumSeats = 0; FlightMaxWeight = 0; }; }