Esempio n. 1
0
        /// <summary>
        /// 泛型方法,反射生成UpdateSql语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="pkName">主键</param>
        /// <param name="where">自定义条件</param>
        /// <returns>int</returns>
        public static StringBuilder UpdateSql <T>(T entity, string pkName = "", string where = "")
        {
            Type type = entity.GetType();
            //表名
            string table = EntityAttributeHelper.GetEntityTable <T>();
            //主键
            string keyField = EntityAttributeHelper.GetEntityKey <T>();
            //获取不做映射的字段
            List <string> notMappedField = EntityAttributeHelper.GetNotMappedFields <T>();

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

            sb.Append(" Update ");
            sb.Append(table);
            sb.Append(" Set ");
            bool isFirstValue = true;

            foreach (PropertyInfo prop in props)
            {
                if (!notMappedField.Contains(prop.Name))
                {
                    if (prop.GetValue(entity, null) != null && keyField != 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);
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(pkName))
            {
                sb.Append(" Where ").Append(pkName).Append("=").Append(DbParameters.CreateDbParmCharacter() + pkName);
            }
            else if (!string.IsNullOrEmpty(where))
            {
                sb.Append(" " + where);
            }
            return(sb);
        }
Esempio n. 2
0
        /// <summary>
        /// 泛型方法,反射生成UpdateSql语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="where">更新条件</param>
        /// <returns>int</returns>
        public static StringBuilder UpdateSql <T>(T entity, string where = "")
        {
            Type type = entity.GetType();
            //表名
            string table = EntityAttributeHelper.GetEntityTable <T>();
            //主键
            string keyField = EntityAttributeHelper.GetEntityKey <T>();
            //获取不做映射的字段
            List <string> notMappedField = EntityAttributeHelper.GetNotMappedFields <T>();

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

            sb.Append("Update ");
            sb.Append(table);
            sb.Append(" Set ");
            bool isFirstValue = true;

            foreach (PropertyInfo prop in props)
            {
                if (!notMappedField.Contains(prop.Name))
                {
                    object value = prop.GetValue(entity, null);
                    if (value != null && keyField != prop.Name)
                    {
                        if (value.GetType() == typeof(DateTime))
                        {
                            var time = (DateTime)value;
                            if (time.Ticks != 0 && time.Kind != DateTimeKind.Unspecified)
                            {
                                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);
                                }
                            }
                        }
                        else
                        {
                            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(keyField).Append("=").Append(DbParameters.CreateDbParmCharacter() + keyField);
            return(sb);
        }