コード例 #1
0
ファイル: DALHelper.cs プロジェクト: lonlywaiting/Netcollect
        /// <summary>
        /// 对单表进行Insert操作
        /// </summary>
        /// <typeparam name="T">泛型T</typeparam>
        /// <param name="model">实体参数</param>
        /// <param name="tran">事务</param>
        /// <returns>返回影响的行数</returns>
        public int Insert <T>(T model) where T : class
        {
            string sql       = string.Empty;
            string tableName = model.GetType().Name;

            object[] customAttribute = null;
            object[] tableAttribute  = null;

            Type t = model.GetType();

            tableAttribute = t.GetCustomAttributes(typeof(TableAttribute), false);
            if (tableAttribute != null && tableAttribute.Length > 0)
            {
                tableName = (tableAttribute[0] as TableAttribute).TableName;
            }

            sql = "insert into " + tableName;
            StringBuilder fieldList = new StringBuilder();
            StringBuilder valueList = new StringBuilder();

            PropertyInfo[] pis = t.GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                if (pi.GetValue(model, null) == null)//如果属性值为null 不插入
                {
                    continue;
                }

                customAttribute = pi.GetCustomAttributes(typeof(ColumnAttribute), false);
                if (customAttribute.Count() == 1 && (customAttribute[0] as ColumnAttribute).NotCreateWhere)
                {
                    continue;
                }
                if (customAttribute.Count() == 0 || (customAttribute.Count() == 1 && !(customAttribute[0] as ColumnAttribute).Identity))
                {
                    fieldList.AppendFormat(",{0}", pi.Name);
                    valueList.AppendFormat(",@{0}", pi.Name);
                }
            }
            if (fieldList.Length == 0 || valueList.Length == 0)
            {
                throw new ArgumentException();
            }

            sql = string.Format("insert into {0}({1}) values({2})", tableName, fieldList.Remove(0, 1), valueList.Remove(0, 1));

            using (var db = new LcLib.DBLib.DBHelper())
            {
                return(db.ExecuteNonQuery(sql, model));
            }
        }
コード例 #2
0
ファイル: DALHelper.cs プロジェクト: lonlywaiting/Netcollect
        /// <summary>
        /// 对单表进行Update操作
        /// </summary>
        /// <typeparam name="T">泛型T</typeparam>
        /// <param name="model">实体参数</param>
        /// <param name="tran">事务</param>
        /// <returns>返回影响的行数</returns>
        public int Update <T>(T model) where T : class
        {
            string sql       = string.Empty;
            string tableName = model.GetType().Name;
            Type   t         = model.GetType();

            object[] customAttribute = null;
            object[] tableAttribute  = null;

            tableAttribute = t.GetCustomAttributes(typeof(TableAttribute), false);
            if (tableAttribute != null && tableAttribute.Length > 0)
            {
                tableName = (tableAttribute[0] as TableAttribute).TableName;
            }

            StringBuilder keyValuePaire = new StringBuilder();
            StringBuilder condition     = new StringBuilder();

            PropertyInfo[] pis = t.GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                if (pi.GetValue(model, null) == null)//如果属性值为null 不更新
                {
                    continue;
                }
                customAttribute = pi.GetCustomAttributes(typeof(ColumnAttribute), false);
                if (customAttribute.Count() == 1 && (customAttribute[0] as ColumnAttribute).NotCreateWhere)
                {
                    continue;
                }
                if (customAttribute.Count() == 1 && (customAttribute[0] as ColumnAttribute).PrimaryKey)
                {
                    condition.AppendFormat(" and {0}=@{0}", pi.Name);
                }
                else
                {
                    keyValuePaire.AppendFormat(",{0}=@{0}", pi.Name);
                }
            }
            if (condition.Length == 0 || keyValuePaire.Length == 0)
            {
                throw new ArgumentException();
            }

            sql = string.Format("update {0} set {1} where 1=1 {2}", tableName, keyValuePaire.Remove(0, 1), condition);

            using (var db = new LcLib.DBLib.DBHelper())
            {
                return(db.ExecuteNonQuery(sql, model));
            }
        }