예제 #1
0
 ///// <summary>
 /// 得到一个Model,根据主键查询
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="ID"></param>
 /// <returns></returns>
 public static T GetModel <T>(object ID) where T : new()
 {
     try
     {
         Type   type      = typeof(T);
         string tableName = GetTableName(type);
         //type.getp
         PropertyInfo[] propertyArray = type.GetProperties(); //获取所有的公有属性
         StringBuilder  strSql        = new StringBuilder();
         strSql.Append("SELECT ");
         //List<string> keys = new List<string>();
         //暂时只支持一个主键
         string keys = string.Empty;
         List <SqlParameter> param = new List <SqlParameter>();
         string tempPropName       = string.Empty;
         foreach (PropertyInfo property in propertyArray)
         {
             tempPropName = property.Name;
             object[] objArray = property.GetCustomAttributes(typeof(EnitityMappingAttribute), true);
             if (objArray.Length > 0)
             {
                 EnitityMappingAttribute propAttr = objArray[0] as EnitityMappingAttribute;
                 if (propAttr.ColumnType == "IGNORE")
                 {
                     continue;
                 }
                 else if (propAttr.ColumnType == "KEY")
                 {
                     keys = tempPropName;
                     param.Add(new SqlParameter("@" + tempPropName, ID));
                     //break;
                 }
             }
             strSql.Append(tempPropName + ",");
         }
         if (string.IsNullOrEmpty(keys))
         {
             throw new Exception("未设置主键");
         }
         strSql.Remove(strSql.Length - 1, 1);
         strSql.Append("  from  " + tableName + " where ");
         strSql.Append(keys + "=@" + keys);
         DataSet ds = DbHelperSQL.Query(strSql.ToString(), param.ToArray());
         //实体转换
         if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
         {
             T t = new T();
             t = TranEntity <T>(propertyArray, ds.Tables[0].Rows[0]);
             return(t);
         }
         return(default(T));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
예제 #2
0
        /// <summary>
        /// 得到一个Model,根据主键查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static T GetModel <T>(object ID, string[] fieldsName) where T : new()
        {
            Type   type      = typeof(T);
            string tableName = GetTableName(type);

            PropertyInfo[]      propertyArray = type.GetProperties(); //获取所有的公有属性
            List <PropertyInfo> propList      = new List <PropertyInfo>();
            StringBuilder       strSql        = new StringBuilder();

            strSql.Append("SELECT ");
            //List<string> keys = new List<string>();
            //暂时只支持一个主键
            string keys = string.Empty;
            List <SqlParameter> param = new List <SqlParameter>();
            string tempPropName       = string.Empty;

            foreach (PropertyInfo property in propertyArray)
            {
                tempPropName = property.Name;
                object[] objArray = property.GetCustomAttributes(typeof(EnitityMappingAttribute), true);
                if (objArray.Length > 0)
                {
                    EnitityMappingAttribute propAttr = objArray[0] as EnitityMappingAttribute;
                    if (propAttr.ColumnType == "IGNORE")
                    {
                        continue;
                    }
                    else if (propAttr.ColumnType == "KEY")
                    {
                        keys = tempPropName;
                        param.Add(new SqlParameter("@" + tempPropName, ID));
                    }
                }
                if (fieldsName.Length > 0 && fieldsName.Contains(tempPropName))
                {
                    strSql.Append(tempPropName + ",");
                    propList.Add(property);
                }
            }
            strSql.Remove(strSql.Length - 1, 1);
            strSql.Append("  from  " + tableName + " where ");
            strSql.Append(keys + "=@" + keys);

            //new DbHelperSQL(PubConstant.CompanyConnectionString);
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), param.ToArray());

            //实体转换
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                T t = new T();
                t = TranEntity <T>(propertyArray, ds.Tables[0].Rows[0]);
                return(t);
            }
            return(default(T));
        }
예제 #3
0
        /// <summary>
        /// 更新一个实体,添加到事务列表中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="comm"></param>
        /// <returns></returns>
        public static List <CommonObject> Update <T>(T model, List <CommonObject> comm)
        {
            try
            {
                StringBuilder strSql    = new StringBuilder();
                Type          type      = typeof(T);
                string        tableName = GetTableName(type);

                strSql.Append("update " + tableName + " set ");
                PropertyInfo[]      propertyArray = type.GetProperties(); //获取所有的公有属性
                string              keys = string.Empty, tempPropName = string.Empty;
                List <SqlParameter> param = new List <SqlParameter>();

                foreach (PropertyInfo property in propertyArray)
                {
                    //if (string.IsNullOrEmpty(keys))
                    //    keys = GetTableKey(property, tempPropName);
                    ////如果主键是Int或long的自增类型,就不更新该字段
                    //if ((!string.IsNullOrEmpty(keys) && property.PropertyType.FullName == "System.Int32") || (!string.IsNullOrEmpty(keys) && property.PropertyType.FullName == "System.Int64"))
                    //    continue;
                    object[] objArray = property.GetCustomAttributes(typeof(EnitityMappingAttribute), true);
                    if (objArray.Length > 0)
                    {
                        EnitityMappingAttribute propAttr = objArray[0] as EnitityMappingAttribute;
                        if (propAttr.ColumnType == "IGNORE")
                        {
                            continue;
                        }
                        if (propAttr.ColumnType == "KEY")
                        {
                            keys = property.Name;// GetTableKey(property, tempPropName);
                            param.Add(new SqlParameter("@" + keys, property.FastGetValue(model)));
                            continue;
                        }
                    }
                    tempPropName = property.Name;
                    strSql.Append(tempPropName + " = @" + tempPropName + ",");
                    param.Add(new SqlParameter("@" + tempPropName, property.FastGetValue(model)));
                }
                strSql.Remove(strSql.Length - 1, 1);
                strSql.Append(" where ");
                strSql.Append(keys + " = @" + keys);
                comm.Add(new CommonObject(strSql.ToString(), param.ToArray()));
                return(comm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #4
0
        /// <summary>
        /// 插入一个实体,添加到执行事务列表中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="comm"></param>
        /// <returns></returns>
        public static List <CommonObject> Insert <T>(T model, List <CommonObject> comm)
        {
            try
            {
                StringBuilder strSql    = new StringBuilder();
                Type          type      = typeof(T);
                string        tableName = GetTableName(type);
                strSql.Append("insert into " + tableName + "( ");
                string insertFields = string.Empty, insertFieldParams = string.Empty, tempPropName = string.Empty;
                List <SqlParameter> param         = new List <SqlParameter>();
                PropertyInfo[]      propertyArray = type.GetProperties(); //获取所有的公有属性
                string keys = string.Empty;
                foreach (PropertyInfo property in propertyArray)
                {
                    tempPropName = property.Name;
                    //如果主键是Int或long的自增类型,就不插入
                    if (string.IsNullOrEmpty(keys))
                    {
                        keys = GetTableKey(property, tempPropName);
                        if ((!string.IsNullOrEmpty(keys) && property.PropertyType.FullName == "System.Int32") || (!string.IsNullOrEmpty(keys) && property.PropertyType.FullName == "System.Int64"))
                        {
                            continue;
                        }
                    }
                    object[] objArray = property.GetCustomAttributes(typeof(EnitityMappingAttribute), true);
                    if (objArray.Length > 0)
                    {
                        EnitityMappingAttribute propAttr = objArray[0] as EnitityMappingAttribute;
                        if (propAttr.ColumnType == "IGNORE")
                        {
                            continue;
                        }
                    }

                    insertFields      += tempPropName + ",";
                    insertFieldParams += "@" + tempPropName + ",";
                    param.Add(new SqlParameter("@" + tempPropName, property.FastGetValue(model)));
                }
                insertFields      = insertFields.Remove(insertFields.Length - 1, 1);
                insertFieldParams = insertFieldParams.Remove(insertFieldParams.Length - 1, 1);
                strSql.Append(insertFields + ") values (" + insertFieldParams + ") ;");
                comm.Add(new CommonObject(strSql.ToString(), param.ToArray()));
                return(comm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #5
0
        /// <summary>
        /// 获取主键列
        /// </summary>
        /// <param name="property"></param>
        /// <param name="tempPropName"></param>
        /// <returns></returns>
        private static string GetTableKey(PropertyInfo property, string tempPropName)
        {
            string keys = string.Empty;

            object[] objArray = property.GetCustomAttributes(typeof(EnitityMappingAttribute), true);
            if (objArray.Length > 0)
            {
                EnitityMappingAttribute propAttr = objArray[0] as EnitityMappingAttribute;
                if (propAttr.ColumnType.Equals("KEY"))
                {
                    keys = tempPropName;
                }
            }
            return(keys);
        }
예제 #6
0
        /// <summary>
        /// 获取表明
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static string GetTableName(Type type)
        {
            string tableName = string.Empty;

            object[] objArray = type.GetCustomAttributes(typeof(EnitityMappingAttribute), false);
            if (objArray.Length > 0)
            {
                EnitityMappingAttribute attr = objArray[0] as EnitityMappingAttribute;
                tableName = attr.TableName;
            }
            else
            {
                tableName = type.Name;
            }
            return(tableName);
        }