Example #1
0
        /// <summary>
        /// 生成更新SQL语句
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns>返回</returns>
        public static string ColumnSqlForUpdate(object entity, PropertyInfo propertyInfo, List <SqlParameter> parameter)
        {
            var config        = EntityParser.GetColumnConfig(propertyInfo);       //获取对属性/列的配置信息
            var propertyValue = propertyInfo.GetValue(entity, null);

            if (config.UsePropertyForUpdate || propertyValue == null)               //如果使用属性的值更新数据库中的值
            {
                var columnName      = EntityParser.GetColumnName(propertyInfo);
                var valueExpression = "@" + columnName;

                if (config.DBType == DBType.PropertyType)                  //如果数据库中的类型 和 属性的类型相同
                {
                    parameter.Add(new SqlParameter(valueExpression, propertyValue));
                }
                else                   //如果要转换类型
                {
                    propertyValue = EntityParser.ConvertPropertyValue(propertyInfo, propertyValue, config.DBType);
                    parameter.Add(new SqlParameter(valueExpression, propertyValue));
                }
                return(valueExpression);
            }
            else
            {
                if (!string.IsNullOrEmpty(config.UpdateSQL))                  //如果不用属性,就使用特定的SQL语句指定列的值
                {
                    return(config.UpdateSQL);
                }
            }

            return(string.Empty);
        }
Example #2
0
        /// <summary>
        /// 返回null或空字符串表示不生成SQL语句
        /// </summary>
        /// <returns></returns>
        public static string ColumnSqlForInsert(object entity, PropertyInfo propertyInfo, List <SqlParameter> parameter)
        {
            var config = EntityParser.GetColumnConfig(propertyInfo);

            if (config.UsePropertyForInsert)
            {
                var columnName      = EntityParser.GetColumnName(propertyInfo);
                var valueExpression = "@" + columnName;
                var propertyValue   = propertyInfo.GetValue(entity, null);
                if (config.DBType == DBType.PropertyType)                  //如果直接用属性
                {
                    parameter.Add(new SqlParameter(valueExpression, propertyValue));
                }
                else                   //如果要转换类型
                {
                    propertyValue = EntityParser.ConvertPropertyValue(propertyInfo, propertyValue, config.DBType);
                    parameter.Add(new SqlParameter(valueExpression, propertyValue));
                }
                return(valueExpression);
            }
            else
            {
                if (!string.IsNullOrEmpty(config.InsertSQL))
                {
                    return(config.InsertSQL);
                }
            }

            return(string.Empty);
        }
Example #3
0
        /// <summary>
        /// 向数据库中插入一个实体并返回实体类的主键的值,并从数据库中刷新实体的所有值
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity">要插入到数据库的实体类的实例</param>
        /// <returns></returns>
        public static TEntity Insert <TEntity>(TEntity entity) where TEntity : SQLEntity
        {
            var entityType = typeof(TEntity);
            List <ColumnContext> contexts;

            if (!insertCache.TryGetValue(entityType, out contexts))
            {
                contexts = new List <ColumnContext>();

                foreach (var propertyInfo in entityType.GetProperties())
                {
                    if (EntityParser.IsIgnore(propertyInfo))
                    {
                        continue;
                    }
                    var config  = EntityParser.GetColumnConfig(propertyInfo);
                    var context = new ColumnContext()
                    {
                        Config       = config,
                        PropertyInfo = propertyInfo,
                        PropertyName = propertyInfo.Name,
                        ColumnName   = EntityParser.GetColumnName(propertyInfo)
                    };
                    contexts.Add(context);
                }
                insertCache.Add(entityType, contexts);
            }


            List <SqlParameter> parameters = new List <SqlParameter>();

            foreach (var context in contexts)
            {
                if (context.Config.UsePropertyForInsert)
                {
                    var valueExpression = "@" + context.PropertyName;
                    var propertyValue   = context.PropertyInfo.GetValue(entity, null);
                    if (context.Config.DBType == DBType.PropertyType)                      //如果直接用属性
                    {
                        parameters.Add(new SqlParameter(valueExpression, propertyValue));
                    }
                    else                       //如果要转换类型
                    {
                        propertyValue = EntityParser.ConvertPropertyValue(context.PropertyInfo, propertyValue, context.Config.DBType);
                        parameters.Add(new SqlParameter(valueExpression, propertyValue));
                    }
                }
            }

            var    keyContext = EntityParser.GetKey(entityType);            //寻找主键特性
            object key;

            string sql = SqlBuilder.InsertSQL(entityType);

            if (keyContext.KeyConfig.UsePropertyForInsert)               //如果使用属性的值插入到数据库
            {
                SQLServerHelper.ExecuteNonQuery(sql, parameters.ToArray());
                key = keyContext.Key.GetValue(entity, null);
            }
            else
            {
                key = SQLServerHelper.GetSingle(sql, parameters.ToArray());                   //插入并获取主键的值
            }


            var se = entity as SQLEntity;

            if (se != null)
            {
                se.EntityState = EntityState.NotInitialized;                   //未初始化
            }

            string selectSQL = SqlBuilder.SelectSQL(typeof(TEntity), string.Format("{0} = @{0}", keyContext.PropertyName));

            SQLServerHelper.FirstOrDefault((reader) =>
            {
                return(FilldEntity <TEntity>(reader, entity));
            }, selectSQL, new SqlParameter("@" + keyContext.PropertyName, key));              //从数据库中刷新实体

            if (se != null)
            {
                se.EntityState = EntityState.NotChanged;                  //未修改
            }
            return(entity);
        }