public void Delete(int id) { EventProps props = new EventProps(); props.ID = id; Delete(props); }
} // end of Create() /// <summary> /// </summary> public bool Delete(IBaseProps p) { EventProps props = (EventProps)p; List <EventProps> events = new List <EventProps>(); try { events = (List <EventProps>)RetrieveAll(events.GetType()); int index = IndexOf(events, props.ID); if (index != -1) { events.RemoveAt(index); WriteAll(events); return(true); } else { throw new Exception("Event with id of " + props.ID.ToString() + " does not exist"); } } catch (Exception e) { // log the error throw; } finally { } } // end of Delete()
} //end of Retrieve() // retrieves a list of objects public object RetrieveAll(Type type) { List <EventProps> list = new List <EventProps>(); DBDataReader reader = null; EventProps props; try { reader = RunProcedure("usp_EventSelectAll"); if (!reader.IsClosed) { while (reader.Read()) { props = new EventProps(); props.SetState(reader); list.Add(props); } } return(list); } catch (Exception e) { // log this exception throw; } finally { if (!reader.IsClosed) { reader.Close(); } } }
} // end of Retrieve() #endregion #region IWriteDB Members /// <summary> /// </summary> public IBaseProps Create(IBaseProps p) { EventProps props = (EventProps)p; List <EventProps> events = new List <EventProps>(); try { events = (List <EventProps>)RetrieveAll(events.GetType()); props.ID = NextID(events); props.ConcurrencyID = 1; events.Add(props); WriteAll(events); return(props); } catch (Exception e) { // log the error throw; } finally { } } // end of Create()
public void TestPropsRetrieve() { EventSQLDB db = new EventSQLDB(dataSource); EventProps props = (EventProps)db.Retrieve(2); Assert.AreEqual(props.ID, 2); Console.WriteLine(props.GetState()); }
} // end of Delete() /// <summary> /// </summary> public bool Update(IBaseProps p) { int rowsAffected = 0; EventProps props = (EventProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_EventUpdate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@EventID", SqlDbType.Int); command.Parameters.Add("@UserID", SqlDbType.Int); command.Parameters.Add("@EventTitle", SqlDbType.NVarChar); command.Parameters.Add("@EventDescription", SqlDbType.NVarChar); command.Parameters.Add("@EventDate", SqlDbType.Date); command.Parameters.Add("@ConcurrencyID", SqlDbType.Int); command.Parameters["@EventID"].Value = props.ID; command.Parameters["@UserID"].Value = props.userID; command.Parameters["@EventTitle"].Value = props.title; command.Parameters["@EventDescription"].Value = props.description; command.Parameters["@EventDate"].Value = props.date; command.Parameters["@ConcurrencyID"].Value = props.ConcurrencyID; try { rowsAffected = RunNonQueryProcedure(command); if (rowsAffected == 1) { props.ConcurrencyID++; return(true); } else { string message = "Record cannot be updated. It has been edited by another user."; throw new Exception(message); } } catch (Exception e) { // log this exception throw; } finally { if (mConnection.State == ConnectionState.Open) { mConnection.Close(); } } } // end of Update()
/// <summary> /// </summary> public IBaseProps Create(IBaseProps p) { int rowsAffected = 0; EventProps props = (EventProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_EventCreate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@EventID", SqlDbType.Int); command.Parameters.Add("@UserID", SqlDbType.Int); command.Parameters.Add("@EventTitle", SqlDbType.NVarChar); command.Parameters.Add("@EventDescription", SqlDbType.NVarChar); command.Parameters.Add("@EventDate", SqlDbType.Date); command.Parameters[0].Direction = ParameterDirection.Output; command.Parameters["@UserID"].Value = props.userID; command.Parameters["@EventTitle"].Value = props.title; command.Parameters["@EventDescription"].Value = props.description; command.Parameters["@EventDate"].Value = props.date; try { rowsAffected = RunNonQueryProcedure(command); if (rowsAffected == 1) { props.ID = (int)command.Parameters[0].Value; props.ConcurrencyID = 1; return(props); } else { throw new Exception("Unable to insert record. " + props.ToString()); } } catch (Exception e) { // log this error throw; } finally { if (mConnection.State == ConnectionState.Open) { mConnection.Close(); } } }
/// <summary> /// Instantiates mProps and mOldProps as new Props objects. /// Instantiates mbdReadable and mdbWriteable as new DB objects. /// </summary> protected override void SetUp() { mProps = new EventProps(); mOldProps = new EventProps(); if (this.mConnectionString == "") { mdbReadable = new EventDB(); mdbWriteable = new EventDB(); } else { mdbReadable = new EventDB(this.mConnectionString); mdbWriteable = new EventDB(this.mConnectionString); } }
// *** I deleted IndexOf and NextID. // I'll use the database to find the record and // determine the value of a new EventId // *** The body of all of these methods are different // They use ADO.NET objects and call methods in the SQL base class #region IReadDB Members /// <summary> /// </summary> /// public IBaseProps Retrieve(Object key) { DBDataReader data = null; EventProps props = new EventProps(); DBCommand command = new DBCommand(); command.CommandText = "usp_EventSelect"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@EventID", SqlDbType.Int); command.Parameters["@EventID"].Value = (Int32)key; try { data = RunProcedure(command); // *** I added this version of SetState to the props class if (!data.IsClosed) { if (data.Read()) { props.SetState(data); } else { throw new Exception("Record does not exist in the database."); } } return(props); } catch (Exception e) { // log this exception throw; } finally { if (data != null) { if (!data.IsClosed) { data.Close(); } } } } //end of Retrieve()
} // end of Delete() /// <summary> /// </summary> public bool Update(IBaseProps p) { EventProps props = (EventProps)p; List <EventProps> events = new List <EventProps>(); try { events = (List <EventProps>)RetrieveAll(events.GetType()); int index = IndexOf(events, props.ID); if (index != -1) { if (props.ConcurrencyID == events[index].ConcurrencyID) { events.RemoveAt(index); props.ConcurrencyID++; events.Add(props); WriteAll(events); return(true); } else { throw new Exception("Event with id of " + props.ID.ToString() + " appears to have been edited by another user. Changes can not be saved."); } } else { throw new Exception("Event with id of " + props.ID.ToString() + " does not exist"); } } catch (Exception e) { // log this error throw; } finally { } } // end of Update()
public Customer(EventProps props, string cnString) : base(props, cnString) { }
// *** I added these 2 so that I could create a // business object from a properties object // I added the new constructors to the base class public Customer(EventProps props) : base(props) { }
public Event(EventProps props, string cnString) : base(props, cnString) { }
// *** I added these 2 so that I could create a // business object from a properties object // I added the new constructors to the base class public Event(EventProps props) : base(props) { }