Ejemplo n.º 1
0
        /// <summary>
        /// 插入一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int INSERT <T>(T t) where T : BaseModel
        {
            if (t == null)
            {
                return(0);
            }
            Type type = typeof(T);

            foreach (var property in type.GetProperties())
            {
                if (property.IsDefined(typeof(IAttributeCheck), true))
                {
                    var temp = property.GetCustomAttributes(typeof(IAttributeCheck), true)
                               .FirstOrDefault(item => item is IAttributeCheck);
                    if (temp is IAttributeCheck)
                    {
                        IAttributeCheck attribute = temp as IAttributeCheck;
                        if (!attribute.Check(property.GetValue(t)))
                        {
                            return(0);
                        }
                    }
                }
            }

            Func <T, int> func = (a =>
            {
                connection.Open();  //open the connection

                string cmd = "INSERT INTO `" + getClassName(type) + "` ";
                List <string> PropertyList = new List <string>();
                List <string> ValueList = new List <string>();
                foreach (var property in type.GetProperties())
                {
                    if (GetPropertyName(property) == "id")
                    {
                        break;
                    }
                    if (property.GetValue(t) != null)
                    {
                        PropertyList.Add(GetPropertyName(property));
                        ValueList.Add("'" + property.GetValue(t).ToString() + "'");
                    }
                }
                var Properties = String.Join(",", PropertyList);
                var Values = String.Join(",", ValueList);
                cmd += "(" + Properties + ")" + " VALUES " + "(" + Values + ")";
                MySqlCommand mysqlcom = new MySqlCommand(cmd, connection);
                int count = mysqlcom.ExecuteNonQuery();
                //    INSERT INTO `user` (name, num) VALUES('刘九', '1231564')
                connection.Close();
                return(count);
            });

            return(GenericCache <CallbackHelper> .GetCache().ThreadWithArgeWithRetrun(func).Invoke());
        }
Ejemplo n.º 2
0
        public int Update <T>(T t) where T : BaseModel
        {
            if (t == null)
            {
                return(0);
            }
            Type type = typeof(T);

            foreach (var property in type.GetProperties())
            {
                if (property.IsDefined(typeof(IAttributeCheck), true))
                {
                    var temp = property.GetCustomAttributes(typeof(IAttributeCheck), true)
                               .FirstOrDefault(item => item is IAttributeCheck);
                    if (temp is IAttributeCheck)
                    {
                        IAttributeCheck attribute = temp as IAttributeCheck;
                        if (!attribute.Check(property.GetValue(t)))
                        {
                            return(0);
                        }
                    }
                }
            }


            int id = -1;

            foreach (var property in type.GetProperties())
            {
                if (GetPropertyName(property) == "id")
                {
                    if (property.GetValue(t) != null)
                    {
                        int.TryParse(property.GetValue(t).ToString(), out id);
                        break;
                    }
                }
            }

            if (id == -1)
            {
                return(0);
            }
            if (Read <T>(id).Count > 0)
            {
                Func <T, int> func = (a =>
                {
                    connection.Open(); //open the connection

                    string cmd = "UPDATE  `" + getClassName(type) + "` SET ";

                    // UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing'

                    List <string> cmdPartList = new List <string>();
                    foreach (var property in type.GetProperties())
                    {
                        if (GetPropertyName(property) == "id")
                        {
                            break;
                        }
                        if (property.GetValue(t) != null)
                        {
                            cmdPartList.Add(GetPropertyName(property) + "=" + "'" + property.GetValue(t).ToString() + "'");
                        }
                    }
                    var cmdparts = String.Join(",", cmdPartList);

                    cmd += cmdparts + " WHERE id = " + id.ToString();
                    MySqlCommand mysqlcom = new MySqlCommand(cmd, connection);
                    int count = mysqlcom.ExecuteNonQuery();
                    //    INSERT INTO `user` (name, num) VALUES('刘九', '1231564')
                    connection.Close();
                    return(count);
                });
                return(GenericCache <CallbackHelper> .GetCache().ThreadWithArgeWithRetrun(func).Invoke());
            }

            return(0);
        }