/// <summary> /// Adds the new return visit. /// </summary> /// <param name="newRv">The new rv.</param> /// <returns><c>True</c> if successful and <c>False</c> if unsuccessful.</returns> /// <exception cref="MyTimeDatabaseLib.ReturnVisitAlreadyExistsException">The Return Visit already exists.</exception> public static bool AddNewReturnVisit(ref ReturnVisitData newRv) { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { var r = ReturnVisitData.Copy(newRv); IQueryable <ReturnVisitDataItem> qry = from x in db.ReturnVisitItems where x.AddressOne.Equals(r.AddressOne) && x.AddressTwo.Equals(r.AddressTwo) && x.City.Equals(r.City) && x.Country.Equals(r.Country) && x.StateProvince.Equals(r.StateProvince) && x.PostalCode.Equals(r.PostalCode) && x.FullName.Equals(r.FullName) select x; if (qry.Any()) { throw new ReturnVisitAlreadyExistsException("The Return Visit already exists.", qry.First().ItemId); } db.ReturnVisitItems.InsertOnSubmit(r); db.SubmitChanges(); newRv.ItemId = r.ItemId; return(newRv.ItemId >= 0); } }
/// <summary> /// Checks the database. /// </summary> public static void CheckDatabase() { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { if (db.DatabaseExists() == false) { db.CreateDatabase(); DatabaseSchemaUpdater dbUpdater = db.CreateDatabaseSchemaUpdater(); dbUpdater.DatabaseSchemaVersion = APP_VERSION; dbUpdater.Execute(); } else { var dbUpdater = db.CreateDatabaseSchemaUpdater(); if (dbUpdater.DatabaseSchemaVersion < 2) { //update from 1.0 to 2.0 db version dbUpdater.AddColumn <ReturnVisitDataItem>("Longitude"); dbUpdater.AddColumn <ReturnVisitDataItem>("Latitude"); dbUpdater.AddColumn <ReturnVisitDataItem>("LastVisitDate"); dbUpdater.DatabaseSchemaVersion = APP_VERSION; dbUpdater.Execute(); var rvList = GetReturnVisits(SortOrder.DateNewestToOldest, -1); foreach (var r in rvList) { var x = r; if (r.LastVisitDate > SqlCeConstants.DateTimeMinValue) { r.LastVisitDate = GetLastVisitDate(ReturnVisitData.Copy(x)); } UpdateReturnVisit(ref x); } } } } }
public static List <ReturnVisitData> GetReturnVisitsByLastVisitDate(int maxReturnCount = 8) { try { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { var qry = from x in db.ReturnVisitItems orderby x.LastVisitDate //where x.LastVisitDate != SqlCeConstants.DateTimeMinValue select x; if (!qry.Any()) { return(null); } bool save = false; foreach (var r in qry) { if (r.LastVisitDate == null) { r.LastVisitDate = GetLastVisitDate(r); save = true; } } if (save) { db.SubmitChanges(); } return(qry.Take(maxReturnCount == -1 ? qry.Count() : maxReturnCount).Select(rv => ReturnVisitData.Copy(rv)).ToList()); } } catch { return(null); } }