Esempio n. 1
0
        /// <summary>
        /// 查询一个对象出来
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="pk">主键值</param>
        /// <returns>一个对象</returns>
        public static T Query <T>(object pk)
        {
            IDataAccess dataAccess = CreateConn();

            if (pk == null || pk.ToString().Length == 0)
            {
                throw new ArgumentException("查询的PK主键不得为空!");
                //return null;
            }

            string    sql = SimpleOrmCache.GetSelectSql(typeof(T)) + " where " + SimpleOrmCache.GetPK(typeof(T)) + "=";
            FieldInfo key = typeof(T).GetField(SimpleOrmCache.GetPK(typeof(T)), BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);

            if (typeof(int) == key.FieldType)
            {
                sql += pk.ToString();
            }
            else
            {
                sql += "'" + pk.ToString() + "'";
            }
            DataTable dt = dataAccess.SelectDataTable(sql, SimpleOrmCache.GetTableName(typeof(T)));

            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                return(CreateFromDataRow <T>(dr));
            }
            else
            {
                return(default(T));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 创建一个对象到数据库中,成功后通过select max()来返回主键
        /// </summary>
        /// <param name="obj">实体对象</param>
        /// <returns>是否插入成功</returns>
        public static bool Create(object obj)
        {
            IDataAccess   dataAccess = CreateConn();
            Type          type       = obj.GetType();
            StringBuilder sql        = new StringBuilder(SimpleOrmCache.GetInsertSql(type));
            Hashtable     table      = SimpleOrmCache.GetInsertField(type);

            System.Collections.IDictionaryEnumerator enumerator = table.GetEnumerator();
            string field = string.Empty;
            object value = null;

            while (enumerator.MoveNext())
            {
                field = enumerator.Key.ToString();
                value = type.GetField(field, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).GetValue(obj);
                sql.Replace("#" + field + "#", value == null ? "" : DALSecurityTool.TransferInsertField(TransObjectField(value)));
            }
            bool result = dataAccess.ExecuteSql(sql.ToString());

            if (result)//执行把主键赋给对象
            {
                string pk     = SimpleOrmCache.GetPK(type);
                object objtmp = dataAccess.SelectScalar("select max(" + pk + ") from " + SimpleOrmCache.GetTableName(type));
                if (objtmp != null)
                {
                    FormHelper.SetDataToObject(obj, pk, objtmp);
                    //FormHelper.SetDataToObject(obj,pk,FormHelper.ParseFieldInfo(obj,objtmp);
                    //FieldInfo fieldtmp=type.GetField(pk);
                    //fieldtmp.SetValue(obj, FormHelper.ParseFieldInfo(fieldtmp, objtmp));
                }
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// 更新一个实体对象
        /// </summary>
        /// <param name="obj">实体对象</param>
        /// <returns>是否更新成功</returns>
        public static bool Update(object obj)
        {
            IDataAccess   dataAccess = CreateConn();
            Type          type       = obj.GetType();
            StringBuilder sql        = new StringBuilder(SimpleOrmCache.GetUpdateSql(type));
            Hashtable     table      = SimpleOrmCache.GetUpdateField(type);

            System.Collections.IDictionaryEnumerator enumerator = table.GetEnumerator();
            string field = string.Empty;
            object value = null;

            while (enumerator.MoveNext())
            {
                field = enumerator.Key.ToString();
                value = type.GetField(field, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).GetValue(obj);
                sql.Replace("#" + field + "#", value == null ? "" : DALSecurityTool.TransferInsertField(TransObjectField(value)));
            }
            FieldInfo key = type.GetField(SimpleOrmCache.GetPK(type), BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);

            if (typeof(int) == key.FieldType)
            {
                sql.Append(key.GetValue(obj).ToString());
            }
            else
            {
                sql.Append("'" + key.GetValue(obj).ToString() + "'");
            }

            return(dataAccess.ExecuteSql(sql.ToString()));
        }
Esempio n. 4
0
 /// <summary>
 /// 比较两实体的主键是否一致,一致认为是一个对象
 /// </summary>
 /// <param name="obj1"></param>
 /// <param name="obj2"></param>
 /// <returns></returns>
 public static bool EntityIsEqual(object obj1, object obj2)
 {
     if (obj1.GetType() != obj2.GetType())
     {
         return(false);
     }
     else
     {
         Type   type   = obj1.GetType();
         string pk     = SimpleOrmCache.GetPK(type);
         object value1 = type.GetField(pk, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).GetValue(obj1);
         object value2 = type.GetField(pk, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).GetValue(obj2);
         return(value1.ToString() == value2.ToString());
     }
 }
Esempio n. 5
0
        public static bool Delete(object entity)
        {
            if (entity == null)
            {
                //throw new ArgumentException("删除的PK主键不得为空!");
                return(false);
            }
            IDataAccess dataAccess = CreateConn();

            Type   type  = entity.GetType();
            string pk    = SimpleOrmCache.GetPK(type);
            object value = type.GetField(pk, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).GetValue(entity);
            string sql   = "delete from " + SimpleOrmCache.GetTableName(type) + " where " + pk + "=" + value.ToString();

            return(dataAccess.ExecuteSql(sql));
        }
Esempio n. 6
0
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="pk">主键id</param>
        /// <returns>是否成功删除</returns>
        public static bool Delete <T>(object pk)
        {
            IDataAccess dataAccess = CreateConn();

            if (pk == null || pk.ToString().Length == 0)
            {
                throw new ArgumentException("删除的PK主键不得为空!");
                //return null;
            }

            string sql = "delete from " + SimpleOrmCache.GetTableName(typeof(T)) + " where " + SimpleOrmCache.GetPK(typeof(T)) + "=" + pk.ToString();

            return(dataAccess.ExecuteSql(sql));
        }