public List <ModelDAL> ModelsFindByFactionID(int FactionID, int Skip, int Take) { // retrieve all Models by FactionID (we expect multiple records here) List <ModelDAL> ReturnValue = new List <ModelDAL>(); CheckConnection(); try { using (SqlCommand command = new SqlCommand("ModelsFindByFactionID", _con)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@FactionID", FactionID); command.Parameters.AddWithValue("@Skip", Skip); command.Parameters.AddWithValue("@Take", Take); using (SqlDataReader reader = command.ExecuteReader()) { ModelMapper mm = new ModelMapper(reader); while (reader.Read()) { ModelDAL item = mm.ToModel(reader); ReturnValue.Add(item); } } } } catch (Exception oops) when(Error.Log(oops)) { //it's all done in the error.log } return(ReturnValue); }
public ModelDAL ModelFindByID(int ModelID) { // retrieve a Model by ID (we dont expect multiple records here) ModelDAL ReturnValue = null; CheckConnection(); try { using (SqlCommand command = new SqlCommand("ModelFindByID", _con)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@ModelID", ModelID); using (SqlDataReader reader = command.ExecuteReader()) { ModelMapper mm = new ModelMapper(reader); int count = 0; while (reader.Read()) { ReturnValue = mm.ToModel(reader); count++; } if (count > 1) { throw new Exception($"{count} Multiple Models found for ID {ModelID}"); } } } } catch (Exception oops) when(Error.Log(oops)) { //it's all done in the error.log } return(ReturnValue); }
public List <ModelDAL> ModelsGetAll(int Skip, int Take) { // retrieves all Models, skip/take are for future paging List <ModelDAL> ReturnValue = new List <ModelDAL>(); CheckConnection(); try { using (SqlCommand command = new SqlCommand("ModelsGetAll", _con)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@skip", Skip); command.Parameters.AddWithValue("@take", Take); using (SqlDataReader reader = command.ExecuteReader()) { ModelMapper mm = new ModelMapper(reader); while (reader.Read()) { ModelDAL item = mm.ToModel(reader); ReturnValue.Add(item); } } } } catch (Exception oops) when(Error.Log(oops)) { //it's all done in the error.log } return(ReturnValue); }
public int ModelCreate(ModelDAL model) { // create a new Model, return the new ModelID int ReturnValue = 0; CheckConnection(); try { using (SqlCommand command = new SqlCommand("ModelCreate", _con)) { // we use a stored procedure, models take a ton of parameters. command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@ModelName", model.ModelName); command.Parameters.AddWithValue("@FactionID", model.FactionID); command.Parameters.AddWithValue("@ModelType", model.ModelType); command.Parameters.AddWithValue("@PointCost", model.PointCost); command.Parameters.AddWithValue("@FullCrewPointCost", model.FullCrewPointCost); command.Parameters.AddWithValue("@AttachesToModelID", model.AttachesToModelID); command.Parameters.AddWithValue("@FieldAllowence", model.FieldAllowence); command.Parameters.AddWithValue("@Speed", model.Speed); command.Parameters.AddWithValue("@MAT", model.MAT); command.Parameters.AddWithValue("@RAT", model.RAT); command.Parameters.AddWithValue("@DEF", model.DEF); command.Parameters.AddWithValue("@ARM", model.ARM); command.Parameters.AddWithValue("@RangedRNG1", model.RangedRNG1); command.Parameters.AddWithValue("@RangedRNG2", model.RangedRNG2); command.Parameters.AddWithValue("@ROF1", model.ROF1); command.Parameters.AddWithValue("@ROF2", model.ROF2); command.Parameters.AddWithValue("@AOE1", model.AOE1); command.Parameters.AddWithValue("@AOE2", model.AOE2); command.Parameters.AddWithValue("@RangedPOW1", model.RangedPOW1); command.Parameters.AddWithValue("@RangedPOW2", model.RangedPOW2); command.Parameters.AddWithValue("@MeleeRNG1", model.MeleeRNG1); command.Parameters.AddWithValue("@MeleeRNG2", model.MeleeRNG2); command.Parameters.AddWithValue("@MeleePOW1", model.MeleePOW1); command.Parameters.AddWithValue("@MeleePOW2", model.MeleePOW2); ReturnValue = Convert.ToInt32(command.ExecuteScalar()); } //we get the new ModelID as a return value } catch (Exception oops) when(Error.Log(oops)) { //it's all done in the error.log } return(ReturnValue); }
public void ModelUpdate(ModelDAL model) { // pretty straightforward. Overwrites data of record of given ModelID(no output) CheckConnection(); try { using (SqlCommand command = new SqlCommand("ModelUpdate", _con)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@ModelID", model.ModelID); command.Parameters.AddWithValue("@ModelName", model.ModelName); command.Parameters.AddWithValue("@FactionID", model.FactionID); command.Parameters.AddWithValue("@ModelType", model.ModelType); command.Parameters.AddWithValue("@PointCost", model.PointCost); command.Parameters.AddWithValue("@FullCrewPointCost", model.FullCrewPointCost); command.Parameters.AddWithValue("@AttachesToModelID", model.AttachesToModelID); command.Parameters.AddWithValue("@FieldAllowence", model.FieldAllowence); command.Parameters.AddWithValue("@Speed", model.Speed); command.Parameters.AddWithValue("@MAT", model.MAT); command.Parameters.AddWithValue("@RAT", model.RAT); command.Parameters.AddWithValue("@DEF", model.DEF); command.Parameters.AddWithValue("@ARM", model.ARM); command.Parameters.AddWithValue("@RangedRNG1", model.RangedRNG1); command.Parameters.AddWithValue("@RangedRNG2", model.RangedRNG2); command.Parameters.AddWithValue("@ROF1", model.ROF1); command.Parameters.AddWithValue("@ROF2", model.ROF2); command.Parameters.AddWithValue("@AOE1", model.AOE1); command.Parameters.AddWithValue("@AOE2", model.AOE2); command.Parameters.AddWithValue("@RangedPOW1", model.RangedPOW1); command.Parameters.AddWithValue("@RangedPOW2", model.RangedPOW2); command.Parameters.AddWithValue("@MeleeRNG1", model.MeleeRNG1); command.Parameters.AddWithValue("@MeleeRNG2", model.MeleeRNG2); command.Parameters.AddWithValue("@MeleePOW1", model.MeleePOW1); command.Parameters.AddWithValue("@MeleePOW2", model.MeleePOW2); object datareturned = command.ExecuteNonQuery(); } } catch (Exception oops) when(Error.Log(oops)) { //it's all done in the error.log } }
public ModelDAL ToModel(SqlDataReader reader) { // lets use the checked offset to write the info to appropriate property ModelDAL ReturnValue = new ModelDAL(); try { // we'll loop through all the info retrieved and let the automapped info sort it for us. for (int i = 0; i < PropertyOffsets.Count; i++) { //we check the type of the property and use the appropriate "get" to minimize garbage string[] property = PropertyOffsets[i].Split(' '); switch (property[0]) { //property contains the type and the name. I split it to check the type case "Int32": typeof(ModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetInt32(i)); break; case "System.String": typeof(ModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetString(i)); break; case "Double": typeof(ModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetDouble(i)); break; case "Char": typeof(ModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetChar(i)); break; default: throw new Exception($"no matching type{property[0]} to sort to in Modelmapper"); //break; } } } catch (Exception oops) when(Error.Log(oops)) { //it's all done in the error.log } #region commented out old code //ReturnValue.ModelID = GetCleanValue(reader, Offsets[0], ReturnValue.ModelID ); //ReturnValue.ModelName = GetCleanValue(reader, Offsets[1], ReturnValue.ModelName ); //ReturnValue.FactionID = GetCleanValue(reader, Offsets[2], ReturnValue.FactionID ); //ReturnValue.FactionName = GetCleanValue(reader, Offsets[3], ReturnValue.FactionName ); //ReturnValue.ModelType = GetCleanValue(reader, Offsets[4], ReturnValue.ModelType); //ReturnValue.PointCost = GetCleanValue(reader, Offsets[5], ReturnValue.PointCost ); //ReturnValue.FullCrewPointCost = GetCleanValue(reader, Offsets[6], ReturnValue.FullCrewPointCost ); //ReturnValue.AttachesToModelID = GetCleanValue(reader, Offsets[7], ReturnValue.AttachesToModelID); //ReturnValue.FieldAllowence = GetCleanValue(reader, Offsets[8], ReturnValue.FieldAllowence ); //ReturnValue.Speed = GetCleanValue(reader, Offsets[9], ReturnValue.Speed ); //ReturnValue.MAT = GetCleanValue(reader, Offsets[10], ReturnValue.MAT ); //ReturnValue.RAT = GetCleanValue(reader, Offsets[11], ReturnValue.RAT ); //ReturnValue.DEF = GetCleanValue(reader, Offsets[12], ReturnValue.DEF ); //ReturnValue.ARM = GetCleanValue(reader, Offsets[13], ReturnValue.ARM ); //ReturnValue.RangedRNG1 = GetCleanValue(reader, Offsets[14], ReturnValue.RangedRNG1 ); //ReturnValue.RangedRNG2 = GetCleanValue(reader, Offsets[15], ReturnValue.RangedRNG2 ); //ReturnValue.ROF1 = GetCleanValue(reader, Offsets[16], ReturnValue.ROF1 ); //ReturnValue.ROF2 = GetCleanValue(reader, Offsets[17], ReturnValue.ROF2 ); //ReturnValue.AOE1 = GetCleanValue(reader, Offsets[18], ReturnValue.AOE1 ); //ReturnValue.AOE2 = GetCleanValue(reader, Offsets[19], ReturnValue.AOE2 ); //ReturnValue.RangedPOW1 = GetCleanValue(reader, Offsets[20], ReturnValue.RangedPOW1 ); //ReturnValue.RangedPOW2 = GetCleanValue(reader, Offsets[21], ReturnValue.RangedPOW2 ); //ReturnValue.MeleeRNG1 = GetCleanValue(reader, Offsets[22], ReturnValue.MeleeRNG1 ); //ReturnValue.MeleeRNG2 = GetCleanValue(reader, Offsets[23], ReturnValue.MeleeRNG2 ); //ReturnValue.MeleePOW1 = GetCleanValue(reader, Offsets[24], ReturnValue.MeleePOW1 ); //ReturnValue.MeleePOW2 = GetCleanValue(reader, Offsets[25], ReturnValue.MeleePOW2); #endregion commented out old code return(ReturnValue); }