Ejemplo n.º 1
0
        /// <summary>
        /// 根据主键进行删除
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="Model">对象</param>
        /// <returns></returns>
        public static bool MySQLDelete <T>(this T Model) where T : new()
        {
            MySQLDBHeper Helper = new MySQLDBHeper();

            PropertyInfo[] propertys = Model.GetType().GetProperties();
            string         ModelName = Model.GetType().Name;
            string         KeyStr    = " and 1=2";
            string         DbValue   = string.Empty;

            foreach (PropertyInfo pi in propertys)
            {
                GDColoum objAttrs = pi.GetCustomAttribute <GDColoum>();
                if (objAttrs != null)
                {
                    if (objAttrs.IsKey)
                    {
                        object obj = pi.GetValue(Model, null);
                        if (obj == null)
                        {
                            DbValue = "''";
                        }
                        else
                        {
                            DbValue = obj.DbTypeStr();
                        }
                        KeyStr = " and " + pi.Name + "=" + DbValue;
                        break;
                    }
                }
            }
            return(Helper.Delete(KeyStr, ModelName));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据条件进行删除
        /// </summary>
        /// <typeparam name="T">对象名称</typeparam>
        /// <param name="Model">对象</param>
        /// <param name="DelCondition">需要删除条件的字段</param>
        /// <returns></returns>
        public static bool MySQLDeleteByConditon <T>(this T Model, string[] DelCondition) where T : new()
        {
            MySQLDBHeper Helper = new MySQLDBHeper();

            PropertyInfo[] propertys = Model.GetType().GetProperties();
            string         ModelName = Model.GetType().Name;
            List <string>  Condition = new List <string>();
            string         DbValue   = string.Empty;

            foreach (PropertyInfo pi in propertys)
            {
                GDColoum objAttrs = pi.GetCustomAttribute <GDColoum>();

                if (objAttrs != null)
                {
                    string Colums = DelCondition.Where(w => w == pi.Name).First();
                    if (!string.IsNullOrEmpty(Colums))
                    {
                        object obj = pi.GetValue(Model, null);
                        if (obj == null)
                        {
                            DbValue = "''";
                        }
                        else
                        {
                            DbValue = obj.DbTypeStr();
                        }
                        Condition.Add("and " + pi.Name + "=" + DbValue);
                    }
                }
            }
            return(Helper.Delete(string.Join(" ", Condition), ModelName));
        }