/// <summary>
        /// DataRow映射到实体
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static T FillModel(DataRow dr)
        {
            if (dr == null)
            {
                return(default(T));
            }

            T model = new T();

            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.GetProperty);
                if (propertyInfo != null && dr[i] != DBNull.Value)
                {
                    object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(SetModelProperty), false);
                    if (customAttributes != null && customAttributes.Length > 0)
                    {
                        SetModelProperty Pty = customAttributes[0] as SetModelProperty;
                        //是否用于查询
                        if (!Pty.IsSelect)
                        {
                            continue;
                        }
                    }
                    propertyInfo.SetValue(model, dr[i], null);
                }
            }
            return(model);
        }
Beispiel #2
0
        /// <summary>
        /// 添加数据库 不缓存数据
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        //public bool NotCacheInsert(IHashObject parameter)
        //{
        //    bool result = false;
        //    using (DbHelper db = new DbHelper(DbControl.GetDbConnStringBasic(DbHandleBehaviorDefine.WRITE, 0)))
        //    {
        //        parameter[this.m_PrimaryFiledKey] = Guid.NewGuid().ToString();
        //        int count = db.Insert(typeof(T).Name, parameter);
        //        if (count > 0)
        //        {
        //            result = true;
        //        }
        //    }
        //    return result;
        //}
        /// <summary>
        /// 添加数据库 不缓存数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //public bool NotCacheInsert(T model)
        //{
        //    IHashObject parameter = ModelToHashObject(model);
        //    return NotCacheInsert(parameter);
        //}

        /// <summary>
        /// 将实体转换为IHashObject
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IHashObject ModelToHashObject(T model)
        {
            IHashObject param = new HashObject();
            Type        t     = model.GetType();

            PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.GetProperty);
            foreach (PropertyInfo p in properties)
            {
                if (p != null)
                {
                    object[] customAttributes = p.GetCustomAttributes(typeof(SetModelProperty), false);
                    if (customAttributes != null && customAttributes.Length > 0)
                    {
                        SetModelProperty Pty = customAttributes[0] as SetModelProperty;
                        //数据库中不存在的字段不添加
                        if (!Pty.DBIsExistFiled)
                        {
                            continue;
                        }
                    }
                }
                param.Add(p.Name, p.GetValue(model, null));
            }
            return(param);
        }
        /// <summary>
        /// 创建实体数据更新数据库的sql语句
        /// </summary>
        /// <param name="model">实体类</param>
        /// <param name="fieldNames">指定条件字段名</param>
        /// <returns></returns>
        ///[Obsolete("不再允许使用此方法", true)]
        public static string CreateUpdateModelSql(T model, params string[] fieldNames)
        {
            Type t;

            PropertyInfo[] properties;
            CheckModel(model, out t, out properties);
            string        modelName = t.Name;
            StringBuilder sql       = new StringBuilder("UPDATE ");

            sql.Append(modelName);
            sql.Append(" SET ");

            StringBuilder whereSql = new StringBuilder();

            foreach (PropertyInfo property in properties)
            {
                object[] customAttributes = property.GetCustomAttributes(typeof(SetModelProperty), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    SetModelProperty Pty = customAttributes[0] as SetModelProperty;
                    //数据库中不存在或者设置不修改该字段
                    if (!Pty.DBIsExistFiled || !Pty.IsUpdate)
                    {
                        continue;
                    }
                }

                object value = property.GetValue(model, null);
                if (GetWhereSql(whereSql, fieldNames, property, value))
                {
                    continue;
                }

                if (value == null)
                {
                    continue;
                }

                sql.Append(property.Name);
                sql.Append("=");

                sql.Append("'");
                sql.Append(ClearSQLKeyChar(value));
                sql.Append("'");
                sql.Append(",");
            }
            sql.Remove(sql.Length - 1, 1);

            if (whereSql.Length > 0)
            {
                whereSql.Remove(whereSql.Length - 5, 5);
            }

            sql.Append(whereSql);

            return(sql.ToString());
        }
        /// <summary>
        /// 创建实体数据插入数据库的sql语句
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns></returns>
        ///[Obsolete("不再允许使用此方法", true)]
        public static string CreateInsertModelSql(T model)
        {
            Type t;

            PropertyInfo[] properties;
            CheckModel(model, out t, out properties);
            string        modelName = t.Name;
            StringBuilder sql       = new StringBuilder("INSERT INTO ");

            sql.Append(modelName);
            sql.Append(" (");

            StringBuilder valueSql = new StringBuilder(" values(");

            foreach (PropertyInfo property in properties)
            {
                object[] customAttributes = property.GetCustomAttributes(typeof(SetModelProperty), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    SetModelProperty Pty = customAttributes[0] as SetModelProperty;
                    //数据库中不存在或者设置不添加该字段
                    if (!Pty.DBIsExistFiled || !Pty.IsInsert)
                    {
                        continue;
                    }
                }

                object value = property.GetValue(model, null);
                if (value == null ||
                    (property.Name.Equals("id", StringComparison.CurrentCultureIgnoreCase)
                     &&
                     (value.ToString() == "0" || value.ToString() == "")
                    )
                    )
                {
                    continue;
                }

                sql.Append(property.Name);
                sql.Append(",");

                valueSql.Append("'");
                valueSql.Append(ClearSQLKeyChar(GetValueByValType(model, property)));
                valueSql.Append("'");
                valueSql.Append(",");
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append(")");

            valueSql.Remove(valueSql.Length - 1, 1);
            valueSql.Append(")");

            sql.Append(valueSql);

            return(sql.ToString());
        }
        /// <summary>
        /// 创建实体数据删除数据库的sql语句
        /// </summary>
        /// <param name="model">实体类</param>
        /// <param name="fieldNames">指定条件字段名</param>
        /// <returns></returns>
        ///[Obsolete("不再允许使用此方法", true)]
        public static string CreateDeleteModelSql(T model, params string[] fieldNames)
        {
            Type t;

            PropertyInfo[] properties;
            CheckModel(model, out t, out properties);
            string        modelName = t.Name;
            StringBuilder sql       = new StringBuilder("DELETE FROM ");

            sql.Append(modelName);

            StringBuilder whereSql = new StringBuilder();

            foreach (PropertyInfo property in properties)
            {
                object[] customAttributes = property.GetCustomAttributes(typeof(SetModelProperty), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    SetModelProperty Pty = customAttributes[0] as SetModelProperty;
                    //数据库中不存在或者设置不通过该字段座位条件删除
                    if (!Pty.DBIsExistFiled || !Pty.IsDelete)
                    {
                        continue;
                    }
                }

                object value = property.GetValue(model, null);
                if (GetWhereSql(whereSql, fieldNames, property, value))
                {
                    continue;
                }
            }

            if (whereSql.Length > 0)
            {
                whereSql.Remove(whereSql.Length - 5, 5);
            }

            sql.Append(whereSql);

            return(sql.ToString());
        }