Example #1
0
        /// <summary>
        /// 新增记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parse2InsertSql">生成完整SQL语句的函数</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        protected bool Insert <T>(Parse2InsertSQL parse2InsertSql, T entity)
        {
            bool isSuccess = false;

            if (entity == null)
            {
                return(isSuccess);
            }

            List <ParamInfo> paramInfos = new List <ParamInfo>(); // 入参实体集合
            string           sql        = AssembleInsertSql <T>(parse2InsertSql, entity, paramInfos);

            if (m_IsDebug)
            {
                Logger.WriteMsg2LogFile(sql);
            }

            object countObj = m_Db.ExecNonQuery(sql, paramInfos);

            if (null != countObj)
            {
                isSuccess = Convert.ToInt32(countObj) != 0;
            }

            return(isSuccess);
        }
Example #2
0
        /// <summary>
        /// 组装Insert语句的Sql
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parse2InsertSql"></param>
        /// <param name="entity"></param>
        /// <param name="paramInfos"></param>
        /// <returns>Insert语句的Sql</returns>
        private string AssembleInsertSql <T>(Parse2InsertSQL parse2InsertSql, T entity, List <ParamInfo> paramInfos)
        {
            // 组装Sql
            string  tblName   = string.Empty; // 表名称
            Type    modelType = typeof(T);
            TblAttr tblAttr   = (TblAttr)modelType.GetCustomAttributes(typeof(TblAttr), true)[0];

            tblName = tblAttr.Name;

            StringBuilder cols      = new StringBuilder(); // 字段名称
            StringBuilder colParams = new StringBuilder(); // 字段参数

            PropertyInfo[] propertyInfos = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            string         realColName   = string.Empty; // 实际的字段名称

            for (int i = 0; i < propertyInfos.Length; i++)
            {
                ColumnAttr[] colAttrs = (ColumnAttr[])propertyInfos[i].GetCustomAttributes(typeof(ColumnAttr), true);
                if (colAttrs == null || colAttrs.Length == 0)
                {
                    continue;
                }
                ColumnAttr colAttr = colAttrs[0];
                if (colAttr.IsPrimary && colAttr.IsAutoGenerate)
                {
                    continue;
                }
                if (propertyInfos[i].GetValue(entity, null) == colAttr.IgnoreValue || object.Equals(propertyInfos[i].GetValue(entity, null), colAttr.IgnoreValue))
                {
                    continue;
                }

                realColName = (StringHelper.IsNullOrWhiteSpace(colAttr.ColName) ? propertyInfos[i].Name : colAttr.ColName);
                cols.AppendFormat("{0},", (realColName.IndexOf(m_QuotingStart) >= 0 ? realColName : m_QuotingStart + realColName + m_QuotingEnd));
                colParams.AppendFormat("{0}{1},"
                                       , m_Db.ParamPreffix
                                       , realColName.Replace(m_QuotingStart.ToString(), "").Replace(m_QuotingEnd.ToString(), ""));
                paramInfos.Add(new ParamInfo(realColName.Replace(m_QuotingStart.ToString(), "").Replace(m_QuotingEnd.ToString(), ""), propertyInfos[i].GetValue(entity, null)));
            }
            if (cols.Length > 0)
            {
                cols.Remove(cols.Length - 1, 1);
            }
            if (colParams.Length > 0)
            {
                colParams.Remove(colParams.Length - 1, 1);
            }

            SQLInsertClause sqlInsertClause = new SQLInsertClause();

            sqlInsertClause.Tbl    = tblName;
            sqlInsertClause.Cols   = cols.ToString();
            sqlInsertClause.Values = colParams.ToString();

            string sql = parse2InsertSql(sqlInsertClause);

            return(sql);
        }
Example #3
0
        /// <summary>
        /// 新增记录并返回自增长主键值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parse2InsertSql">生成完整SQL语句的函数</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        protected object InsertThenReturnID <T>(Parse2InsertSQL parse2InsertSql, T entity)
        {
            object id = null;

            if (entity == null)
            {
                return(id);
            }

            List <ParamInfo> paramInfos = new List <ParamInfo>(); // 入参实体集合
            string           sql        = AssembleInsertSql <T>(parse2InsertSql, entity, paramInfos);

            if (m_IsDebug)
            {
                Logger.WriteMsg2LogFile(sql);
            }

            id = m_Db.ExecScalar(sql, paramInfos);
            return(id);
        }