Example #1
0
        /// <summary>
        /// 泛型方法,反射生成UpdateSql语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>int</returns>
        public static StringBuilder UpdateSql <T>(T entity)
        {
            string pkName = GetKeyField <T>().ToString();
            Type   type   = entity.GetType();

            PropertyInfo[] props = type.GetProperties();
            StringBuilder  sb    = new StringBuilder();

            sb.Append("Update ");
            sb.Append(EntityAttribute.GetTableName <T>());
            sb.Append(" Set ");
            bool isFirstValue = true;

            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null && pkName != prop.Name)
                {
                    if (isFirstValue)
                    {
                        isFirstValue = false;
                        sb.Append(prop.Name);
                        sb.Append("=");
                        sb.Append(DbParameters.CreateDbParmCharacter() + prop.Name);
                    }
                    else
                    {
                        sb.Append("," + prop.Name);
                        sb.Append("=");
                        sb.Append(DbParameters.CreateDbParmCharacter() + prop.Name);
                    }
                }
            }
            sb.Append(" Where ").Append(pkName).Append("=").Append(DbParameters.CreateDbParmCharacter() + pkName);
            return(sb);
        }
Example #2
0
        /// <summary>
        /// 拼接删除SQL语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public static StringBuilder DeleteSql <T>(T entity)
        {
            Type type = entity.GetType();

            PropertyInfo[] props = type.GetProperties();
            StringBuilder  sb    = new StringBuilder("Delete From " + EntityAttribute.GetTableName <T>() + " Where 1=1");

            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null)
                {
                    sb.Append(" AND " + prop.Name + " = " + DbParameters.CreateDbParmCharacter() + "" + prop.Name + "");
                }
            }
            return(sb);
        }
Example #3
0
        /// <summary>
        /// 泛型方法,反射生成InsertSql语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>int</returns>
        public static StringBuilder InsertSql <T>(T entity)
        {
            Type          type = entity.GetType();
            StringBuilder sb   = new StringBuilder();

            sb.Append(" Insert Into ");
            sb.Append(EntityAttribute.GetTableName <T>());
            sb.Append("(");
            StringBuilder sp       = new StringBuilder();
            StringBuilder sb_prame = new StringBuilder();

            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null)
                {
                    sb_prame.Append("," + (prop.Name));
                    sp.Append("," + DbParameters.CreateDbParmCharacter() + "" + (prop.Name));
                }
            }
            sb.Append(sb_prame.ToString().Substring(1, sb_prame.ToString().Length - 1) + ") Values (");
            sb.Append(sp.ToString().Substring(1, sp.ToString().Length - 1) + ")");
            return(sb);
        }
Example #4
0
        public int Update <T>(Expression <Func <T, bool> > condition) where T : class, new()
        {
            bool isTrans = true;

            if (dbTransaction == null)
            {
                BeginTrans();
                isTrans = false;
            }
            IEnumerable <T> entities = dbTransaction.Connection.Query <T>(new SQLinq <T>(EntityAttribute.GetTableName <T>()).Where(condition), null, true, CommandTimeout);

            Update <T>(entities);
            if (!isTrans)
            {
                return(Commit());
            }
            return(0);
        }
Example #5
0
        public int Delete <T>(object keyValue) where T : class
        {
            T entity = dbTransaction.Connection.Query <T>(string.Format("select * from {0} where {1}=@primarykey", EntityAttribute.GetTableName <T>(), EntityAttribute.GetEntityKey <T>()), new { primarykey = keyValue }, null, true, CommandTimeout).FirstOrDefault();

            return(Delete <T>(entity));
        }
Example #6
0
 public int Delete <T>() where T : class
 {
     return(ExecuteBySql(DatabaseCommon.DeleteSql(EntityAttribute.GetTableName <T>()).ToString()));
 }