} // 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()
} // 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()
public void TestUpdate() { IBaseProps temp = DB.Retrieve(2); p = (CustomerProp)temp; p.Name = "changed"; Assert.True(DB.Update(p)); }
// saves to a data store public virtual void Save() { if (mIsDeleted && !mIsNew) { if (mdbWriteable.Delete(mProps)) { mIsDeleted = false; mIsNew = true; mIsDirty = false; SetRequiredRules(); SetDefaultProperties(); mOldProps = (IBaseProps)mProps.Clone(); } } else if (mIsDeleted && mIsNew) { mIsDeleted = false; mIsNew = true; mIsDirty = false; SetRequiredRules(); SetDefaultProperties(); mOldProps = (IBaseProps)mProps.Clone(); } else if (!IsValid) { string message; if (mRules.Count == 1) { message = "Object cannot be saved. One property is invalid."; } else { message = "Object can not be saved. " + mRules.Count + " properties are invalid."; } throw new Exception(message); } else if (mIsNew && !mIsDeleted) { mProps = mdbWriteable.Create(mProps); mIsNew = false; mIsDirty = false; mIsDeleted = false; mRules.Clear(); mOldProps = (IBaseProps)mProps.Clone(); } else if (IsDirty) { if (mdbWriteable.Update(mProps)) { mIsDirty = false; mIsNew = false; mIsDeleted = false; mRules.Clear(); mOldProps = (IBaseProps)mProps.Clone(); } } // end logic related to editing status } // end Save
public bool Update(IBaseProps p) { int rowsAffected = 0; CustomerProps props = (CustomerProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_CustomerUpdate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters.Add("@Name", SqlDbType.VarChar); command.Parameters.Add("@Address", SqlDbType.VarChar); command.Parameters.Add("@City", SqlDbType.VarChar); command.Parameters.Add("@State", SqlDbType.Char); command.Parameters.Add("@ZipCode", SqlDbType.Char); command.Parameters.Add("@ConcurrencyID", SqlDbType.Int); command.Parameters["@CustomerID"].Value = props.ID; command.Parameters["@Name"].Value = props.name; command.Parameters["@Address"].Value = props.address; command.Parameters["@City"].Value = props.city; command.Parameters["@State"].Value = props.state; command.Parameters["@ZipCode"].Value = props.zip; 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) { throw; } finally { if (mConnection.State == ConnectionState.Open) { mConnection.Close(); } } }
// loads from a properties object public virtual void LoadProps(IBaseProps props) { mProps = (IBaseProps)props.Clone(); mOldProps = (IBaseProps)props.Clone(); mIsDirty = false; mIsNew = false; mIsDeleted = false; mRules.Clear(); }
// loads from an xml string public virtual void LoadXML(string xml) { mProps.SetState(xml); mOldProps = (IBaseProps)mProps.Clone(); mIsDirty = false; mIsNew = false; mIsDeleted = false; mRules.Clear(); }
// loads the object from the database based on it's key public virtual void Load(Object key) { mProps = mdbReadable.Retrieve(key); mOldProps = (IBaseProps)mProps.Clone(); mIsDirty = false; mIsNew = false; mIsDeleted = false; mRules.Clear(); }
public IBaseProps Create(IBaseProps p) { int rowsAffected = 0; CustomersProps props = (CustomersProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_CustomersCreate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters.Add("@Name", SqlDbType.VarChar); command.Parameters.Add("@Address", SqlDbType.VarChar); command.Parameters.Add("@City", SqlDbType.VarChar); command.Parameters.Add("@State", SqlDbType.Char); command.Parameters.Add("@ZipCode", SqlDbType.Char); command.Parameters[0].Direction = ParameterDirection.Output; command.Parameters["@Name"].Value = props.name; command.Parameters["@Address"].Value = props.address; command.Parameters["@City"].Value = props.city; command.Parameters["@State"].Value = props.state; command.Parameters["@ZipCode"].Value = props.zipCode; //props.ID = (int) command.Parameters["@ProductID"].Value; 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(); } } }
public IBaseProps Create(IBaseProps p) // possibly might need to change to props to match origional parameter. { int rowsAffected = 0; ProductProps props = (ProductProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_ProductCreate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@ProductID", SqlDbType.Int); command.Parameters.Add("@ProductCode", SqlDbType.Char); command.Parameters.Add("@Description", SqlDbType.VarChar);// these match the parameters int he stored procedure. name adreess etc command.Parameters.Add("@UnitPrice", SqlDbType.Money); command.Parameters.Add("@OnHandQuantity", SqlDbType.Int); // there were three statements here before, it did not include customerID ask about if it needs to be //added // assuming it would be redundant if it wasnt there before.... command.Parameters[0].Direction = ParameterDirection.Output; command.Parameters["@ProductCode"].Value = props.ProductCode; command.Parameters["@Description"].Value = props.Description; command.Parameters["@UnitPrice"].Value = props.UnitPrice; command.Parameters["@OnHandQuantity"].Value = props.OnHandQuantity; 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(); } } }
} //end of Retrieve() #endregion #region IWriteDB Members /// <summary> /// </summary> public IBaseProps Create(IBaseProps p) { //return p; int rowsAffected = 0; ProductProps props = (ProductProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_ProductCreate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@ProductID", SqlDbType.Int); command.Parameters.Add("@ProductCode", SqlDbType.Char); command.Parameters.Add("@Description", SqlDbType.VarChar); command.Parameters.Add("@UnitPrice", SqlDbType.Money); command.Parameters.Add("@OnHandQuantity", SqlDbType.Int); command.Parameters.Add("@ConcurrencyID", SqlDbType.Int); command.Parameters[0].Direction = ParameterDirection.Output; //Should not add value for productID because of autonumber, right? command.Parameters["@ProductCode"].Value = props.code; command.Parameters["@Description"].Value = props.description; command.Parameters["@UnitPrice"].Value = props.unitPrice; command.Parameters["@OnHandQuantity"].Value = props.onHandQty; command.Parameters["@ConcurrencyID"].Value = props.concurrencyID; //This is hardcoded in the stored procedure 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(); } } }
public bool Update(IBaseProps p) { int rowsAffected = 0; ProductProps props = (ProductProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_ProductUpdate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@ProductID", SqlDbType.Int); command.Parameters.Add("@ProductCode", SqlDbType.Char); command.Parameters.Add("@Description", SqlDbType.NVarChar); command.Parameters.Add("@OnHandQuantity", SqlDbType.Int); command.Parameters.Add("@UnitPrice", SqlDbType.Money); command.Parameters.Add("@ConcurrencyID", SqlDbType.Int); command.Parameters["@ProductID"].Value = props.ID; command.Parameters["@ProductCode"].Value = props.code; command.Parameters["@Description"].Value = props.description; command.Parameters["@OnHandQuantity"].Value = props.quantity; command.Parameters["@UnitPrice"].Value = props.price; 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()
public IBaseProps Create(IBaseProps p) { int rowsAffected = 0; ProductProps props = (ProductProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_ProductCreate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@ProductID", SqlDbType.Int); command.Parameters.Add("@ProductCode", SqlDbType.Char); command.Parameters.Add("@Description", SqlDbType.NVarChar); command.Parameters.Add("@OnHandQuantity", SqlDbType.Int); command.Parameters.Add("@UnitPrice", SqlDbType.Money); //Attenetion command.Parameters[0].Direction = ParameterDirection.Output; command.Parameters["@ProductCode"].Value = props.code; command.Parameters["@Description"].Value = props.description; command.Parameters["@OnHandQuantity"].Value = props.quantity; command.Parameters["@UnitPrice"].Value = props.price; 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> /// </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(); } } }
public virtual void UndoChanges() { if (mIsDirty || mIsDeleted) { mProps = (IBaseProps)mOldProps.Clone(); mIsDirty = false; mIsDeleted = false; if (mIsNew) { SetRequiredRules(); } else { mRules.Clear(); } } }
/// <summary> /// </summary> public bool Delete(IBaseProps p) { //return true; ProductProps props = (ProductProps)p; int rowsAffected = 0; DBCommand command = new DBCommand(); command.CommandText = "usp_ProductDelete"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@ProductID", SqlDbType.Int); command.Parameters.Add("@ConcurrencyID", SqlDbType.Int); command.Parameters["@ProductID"].Value = props.id; command.Parameters["@ConcurrencyID"].Value = props.concurrencyID; try { rowsAffected = RunNonQueryProcedure(command); if (rowsAffected == 1) { return(true); } else { string message = "Record cannot be deleted. 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 Delete()
} // 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 bool Delete(IBaseProps props) { ProductsProps x = (ProductsProps)props; ProductsProps temp = (ProductsProps)Retrieve(x.ID); int rowsAffected = 0; ProductsProps p = (ProductsProps)props; DBCommand command = new DBCommand(); command.CommandText = "usp_ProductsDelete"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@ProductID", SqlDbType.Int); command.Parameters["@ProductID"].Value = p.ID; command.Parameters.Add("@ConcurrencyID", SqlDbType.Int); command.Parameters["@ConcurrencyID"].Value = temp.ConcurrencyID; try { rowsAffected = RunNonQueryProcedure(command); if (rowsAffected != 1) { string message = "Record was not deleted. Perhaps the key you specified does not exist."; throw new Exception(message); } return(true); } catch (Exception e) { // log this error throw; } finally { if (mConnection.State == ConnectionState.Open) { mConnection.Close(); } } return(rowsAffected != 0); }
public bool Delete(IBaseProps p) { CustomerProp props = (CustomerProp)p; int rowsAffected = 0; DBCommand command = new DBCommand(); command.CommandText = "usp_CustomerDelete"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters.Add("@ConcurrencyID", SqlDbType.Int); command.Parameters["@CustomerID"].Value = props.ID; command.Parameters["@ConcurrencyID"].Value = props.ConcurrencyID; try { rowsAffected = RunNonQueryProcedure(command); if (rowsAffected == 1) { return(true); } else { string message = "Record cannot be deleted. It has been edited by another user."; throw new Exception(message); } } catch (Exception e) { throw new Exception("Something has gone terribly wrong"); } finally { if (mConnection.State == ConnectionState.Open) { mConnection.Close(); } } }
public bool Delete(IBaseProps props) { DBCommand command = new DBCommand(); command.CommandText = "usp_CustomerDelete"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters["@CustomerID"].Value = ((CustomerProps)props).ID; try { int deleted = RunNonQueryProcedure(command); if (deleted == 1) { return(true); } else { throw new Exception("Record cannot be deleted. It has been edited by another user or does not exist."); } } catch (Exception e) { throw; } finally { if (mConnection.State == ConnectionState.Open) { mConnection.Close(); } } }
// these 2 constructors assume that you have a properties object public BaseBusiness(IBaseProps props, string cnString) { mConnectionString = cnString; SetUp(); LoadProps(props); }
public Product(IBaseProps props) : base(props) { }
public Product(IBaseProps props, string cnString) : base(props, cnString) { }
public Customer(IBaseProps props) : base(props) { }
public bool Update(IBaseProps props) { throw new NotImplementedException(); }
public IBaseProps Create(IBaseProps props) { throw new NotImplementedException(); }
public BaseBusiness(IBaseProps props) { SetUp(); LoadProps(props); }
public IBaseProps Create(IBaseProps p) // possibly might need to change to props to match origional parameter. { // @CustomerID int output, //@Name varchar(100) // @Address varchar(50) // @City varchar(20) // @State char(2) // @ZipCode char(15) int rowsAffected = 0; CustomerProps props = (CustomerProps)p; DBCommand command = new DBCommand(); command.CommandText = "usp_CustomerCreate"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters.Add("@Name", SqlDbType.VarChar);// these match the parameters int he stored procedure. name adreess etc command.Parameters.Add("@Address", SqlDbType.VarChar); command.Parameters.Add("@City", SqlDbType.VarChar); command.Parameters.Add("@State", SqlDbType.Char); command.Parameters.Add("@ZipCode", SqlDbType.Char); command.Parameters[0].Direction = ParameterDirection.Output; // there were three statements here before, it did not include customerID ask about if it needs to be //added // assuming it would be redundant if it wasnt there before.... command.Parameters["@Name"].Value = props.name; command.Parameters["@Address"].Value = props.address; command.Parameters["@City"].Value = props.city; command.Parameters["@State"].Value = props.state; command.Parameters["@ZipCode"].Value = props.zipcode; 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(); } } }
public Customer(IBaseProps props, string cnString) : base(props, cnString) { }