예제 #1
0
        public static bool DropTable(Type type)
        {
            IDataAccess dataAccess = CreateConn();
            string      sql        = "drop table " + SimpleOrmCache.GetTableName(type);

            return(dataAccess.ExecuteSql(sql));
        }
예제 #2
0
        public static bool CreateTable(Type type)
        {
            IDataAccess dataAccess = CreateConn();
            string      sql        = SimpleOrmCache.GetTableDDL(type);

            return(dataAccess.ExecuteSql(sql));
        }
예제 #3
0
        /// <summary>
        /// 查询一个对象列表出来出来
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="sql">完整的sql语句</param>
        /// <returns>一个对象</returns>
        public static ArrayList QueryList(Type type, string sql)
        {
            IDataAccess dataAccess = CreateConn();
            ArrayList   list       = new ArrayList();

            if (sql == null && sql.Length == 0)
            {
                throw new ArgumentException("查询的语句不得为空!");
                //return null;
            }

            // string sql = SimpleOrmCache.GetSelectSql(typeof(T)) + " where " + SimpleOrmCache.GetPK(typeof(T)) + "=" + pk.ToString();
            string    tablename = SimpleOrmCache.GetTableName(type);
            DataTable dt        = dataAccess.SelectDataTable(sql, tablename);

            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = null;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dr = dt.Rows[i];
                    list.Add(CreateFromDataRow(type, dr));
                }
            }
            return(list);
        }
예제 #4
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));
            }
        }
예제 #5
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);
        }
예제 #6
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()));
        }
예제 #7
0
        /// <summary>
        /// 返回查询条件的记录总数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="condition">必须带where的条件</param>
        /// <returns></returns>
        public static int QueryCounts(Type type, string condition)
        {
            IDataAccess dataAccess = CreateConn();
            string      sql        = "select count(*) from " + SimpleOrmCache.GetTableName(type) + condition;
            object      obj        = dataAccess.SelectScalar(sql);

            return(Convert.ToInt32(obj));
        }
예제 #8
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));
        }
예제 #9
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());
     }
 }
예제 #10
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));
        }
예제 #11
0
        /// <summary>
        /// 根据datarow创建一个对象出来
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="dr">datarow</param>
        /// <returns>创建一个对象</returns>
        private static object CreateFromDataRow(Type type, DataRow dr)
        {
            object    result = ReflectHelper.CreateInstance(type);
            Hashtable table  = SimpleOrmCache.GetSelectField(type);

            System.Collections.IDictionaryEnumerator enumerator = table.GetEnumerator();
            object    drvalue   = null;
            FieldInfo fieldInfo = null;

            while (enumerator.MoveNext())
            {
                drvalue   = dr[enumerator.Key.ToString()];
                fieldInfo = result.GetType().GetField(enumerator.Value.ToString(), BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance);
                SetValueToField(result, fieldInfo, drvalue);
            }
            return(result);
        }
예제 #12
0
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="condition">条件语句</param>
        /// <param name="pager">分页对象</param>
        /// <param name="order">排序列</param>
        /// <param name="isDesc">是否降序</param>
        /// <returns></returns>
        public List <T> Query <T>(string condition, Pager pager, string order, bool isDesc)
        {
            IDataAccess dataAccess = CreateConn();
            List <T>    result     = new List <T>();
            string      sql        = dataAccess.GetPageSql(SimpleOrmCache.GetSelectSql(typeof(T)) + condition, pager, order, isDesc);

            DataTable dt = dataAccess.SelectDataTable(sql, SimpleOrmCache.GetTableName(typeof(T)));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    result.Add(CreateFromDataRow <T>(dr));
                }
            }
            return(result);
        }
예제 #13
0
        /// <summary>
        /// 查询一个对象列表出来出来
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="condition">带有where的条件</param>
        /// <returns>一个对象</returns>
        public static ArrayList QueryConditionList(Type type, string condition)
        {
            string sql = "select * from " + SimpleOrmCache.GetTableName(type) + " " + condition;

            return(QueryList(type, sql));
        }