Esempio n. 1
0
        public static void InsertNewRow(Item Item)
        {
            OleDbCommand  command = Connect();
            StringBuilder query   = new StringBuilder();

            query.Append("INSERT INTO [Items](");
            for (int i = 1; i < typeof(Item).GetProperties().Length - 1; i++)
            {
                query.Append(typeof(Item).GetProperties()[i].Name + ",");
            }
            query.Append(typeof(Item).GetProperties().Last().Name);

            query.Append(") VALUES(");



            foreach (PropertyInfo prop in Item.GetType().GetProperties())
            {
                //Kihagyja az első propertit az ID-t
                if (Item.GetType().GetProperties().ToList().IndexOf(prop) == 0)
                {
                    continue;
                }

                var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;

                switch (type.Name)
                {
                case "Boolean":

                    if (bool.Parse(prop.GetValue(Item, null).ToString()))
                    {
                        query.Append("'1', ");
                    }
                    else
                    {
                        query.Append("'0', ");
                    }
                    break;

                case "EnumQuality":
                    EnumQuality enumQuality = (EnumQuality)Enum.Parse(typeof(EnumQuality), prop.GetValue(Item, null).ToString());
                    query.Append("'" + Array.IndexOf(EnumQuality.GetValues(enumQuality.GetType()), enumQuality) + "', ");
                    break;

                case "EnumItemType":
                    EnumItemType enumItemType = (EnumItemType)Enum.Parse(typeof(EnumItemType), prop.GetValue(Item, null).ToString());
                    query.Append("'" + Array.IndexOf(EnumItemType.GetValues(enumItemType.GetType()), enumItemType) + "', ");
                    break;

                case "EnumRarity":
                    EnumRarity enumRarity = (EnumRarity)Enum.Parse(typeof(EnumRarity), prop.GetValue(Item, null).ToString());
                    query.Append("'" + Array.IndexOf(EnumRarity.GetValues(enumRarity.GetType()), enumRarity) + "', ");
                    break;

                case "EnumPotionEffect":
                    EnumPotionEffect enumPotionEffect = (EnumPotionEffect)Enum.Parse(typeof(EnumPotionEffect), prop.GetValue(Item, null).ToString());
                    query.Append("'" + Array.IndexOf(EnumPotionEffect.GetValues(enumPotionEffect.GetType()), enumPotionEffect) + "', ");
                    break;

                case "EnumPossibleBonusStats":
                    EnumPossibleBonusStats enumPossibleBonusStats = (EnumPossibleBonusStats)Enum.Parse(typeof(EnumPossibleBonusStats), prop.GetValue(Item, null).ToString());
                    query.Append("'" + Array.IndexOf(EnumPossibleBonusStats.GetValues(enumPossibleBonusStats.GetType()), enumPossibleBonusStats) + "', ");
                    break;

                default:
                    //Az utolsót külön írja meg mert az utolsónál már le kell zárni a queryt ') -el
                    if (Item.GetType().GetProperties().ToList().IndexOf(prop) == Item.GetType().GetProperties().ToList().IndexOf(Item.GetType().GetProperties().ToList().Last()))
                    {
                        query.Append("'" + prop.GetValue(Item, null).ToString() + "')");
                    }
                    else
                    {
                        query.Append("'" + prop.GetValue(Item, null).ToString() + "', ");
                    }
                    break;
                }
            }

            command.CommandText = query.ToString();
            command.ExecuteNonQuery();
            command.Connection.Close();
        }