예제 #1
0
        public ArmyModelDAL ArmyModelsFindByArmyIDAndModelID(int ArmyID, int ModelID)
        {        //this will bring back a certain model of a certain army, with it's quantities. (expect 1)
            ArmyModelDAL ReturnValue = new ArmyModelDAL();

            CheckConnection();
            try
            {            //try is to catch any problems that could happen when using the SQL connection and log them.
                using (SqlCommand command = new SqlCommand("ArmyModelFindByArmyIDAndModelID", _con))
                {        //Using is used to make sure the heavy recources are disposed properly with dispose
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@ArmyID", ArmyID);
                    command.Parameters.AddWithValue("@ModelID", ModelID);
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        ArmyModelMapper amm   = new ArmyModelMapper(reader);
                        int             count = 0;
                        while (reader.Read())
                        {
                            ReturnValue = amm.ToArmyModel(reader);
                            count++;
                        }
                        if (count > 1)
                        {
                            throw new Exception($"{count} Multiple Models found for ID {ModelID} & {ArmyID}");
                        }
                    }
                }
            }
            catch (Exception oops) when(Error.Log(oops))
            {
                //it's all done in the error.log
            }
            return(ReturnValue);
        }
예제 #2
0
        public ArmyModelDAL ToArmyModel(SqlDataReader reader)
        {           // lets use the checked offset to write the info to appropriate property
            ArmyModelDAL ReturnValue = new ArmyModelDAL();

            try
            {
                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":
                        if (!reader.IsDBNull(i))
                        {
                            typeof(ArmyModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetInt32(i));
                        }
                        else
                        {
                            typeof(ArmyModelDAL).GetProperty(property[1]).SetValue(ReturnValue, 0);
                        }
                        break;

                    case "System.String":
                        typeof(ArmyModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetString(i));
                        break;

                    case "Double":
                        typeof(ArmyModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetDouble(i));
                        break;

                    case "Char":
                        typeof(ArmyModelDAL).GetProperty(property[1]).SetValue(ReturnValue, reader.GetChar(i));
                        break;

                    default:
                        throw new Exception($"no matching type{property[0]} to sort to in ArmyModelmapper");
                        //break;
                    }
                }
            }
            catch (Exception oops) when(Error.Log(oops))
            {
                //it's all done in the error.log
            }
            return(ReturnValue);
        }
예제 #3
0
 public void ArmyModelCreate(ArmyModelDAL Record)
 {        // create a new entry into the many to many table ArmyModels, No Return.
     CheckConnection();
     try
     {            //try is to catch any problems that could happen when using the SQL connection and log them.
         using (SqlCommand command = new SqlCommand("ArmyModelRecordCreate", _con))
         {        //Using is used to make sure the heavy recources are disposed properly with dispose
             command.CommandType = System.Data.CommandType.StoredProcedure;
             command.Parameters.AddWithValue("@ArmyID", Record.ArmyID);
             command.Parameters.AddWithValue("@ModelID", Record.ModelID);
             command.Parameters.AddWithValue("@Quantity", Record.Quantity);
             command.Parameters.AddWithValue("@FullSquats", Record.FullSquats);
             Convert.ToInt32(command.ExecuteNonQuery());                    // the many to many doesn't return a unique ID
         }
     }
     catch (Exception oops) when(Error.Log(oops))
     {
         //it's all done in the error.log
     }
 }