Exemple #1
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override object InsertObject(DbContext dbContext, IModel obj)
        {
            var    helper   = dbContext.DBHelper;
            Type   type     = obj.GetType();
            string table    = TypeCache.GetTableName(type, dbContext);
            var    typeArry = TypeCache.GetProperties(type, true).Values;

            Attribute.FieldAttribute primaryKey = null;
            string sql  = string.Format("insert into `{0}`(", table);
            string sql1 = "";
            string sql2 = "";

            foreach (Attribute.FieldAttribute info in typeArry)
            {
                //if (info.FieldType != Attribute.FieldType.数据库字段)
                //{
                //    continue;
                //}
                string name = info.MapingName;
                if (info.IsPrimaryKey)
                {
                    primaryKey = info;
                }
                if (info.IsPrimaryKey && !info.KeepIdentity)
                {
                    continue;
                }
                //if (!string.IsNullOrEmpty(info.VirtualField))
                //{
                //    continue;
                //}
                object value = info.GetValue(obj);
                if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
                {
                    if (value == null)
                    {
                        continue;
                    }
                }
                value = ObjectConvert.CheckNullValue(value, info.PropertyType);
                sql1 += string.Format("{0},", FieldNameFormat(info));
                sql2 += string.Format("?{0},", name);
                helper.AddParam(name, value);
            }
            sql1 = sql1.Substring(0, sql1.Length - 1);
            sql2 = sql2.Substring(0, sql2.Length - 1);
            sql += sql1 + ") values( " + sql2 + ") ; ";
            sql  = SqlFormat(sql);
            if (primaryKey.KeepIdentity)
            {
                helper.Execute(sql);
                return(primaryKey.GetValue(obj));
            }
            else
            {
                sql += "SELECT LAST_INSERT_ID();";
                return(SqlStopWatch.ExecScalar(helper, sql));
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override int InsertObject(IModel obj)
        {
            Type   type     = obj.GetType();
            string table    = TypeCache.GetTableName(type, dbContext);
            var    typeArry = TypeCache.GetProperties(type, true).Values;

            Attribute.FieldAttribute primaryKey = null;
            string sql  = string.Format("insert into [{0}](", table);
            string sql1 = "";
            string sql2 = "";

            foreach (Attribute.FieldAttribute info in typeArry)
            {
                string name = info.Name;
                if (info.IsPrimaryKey)
                {
                    primaryKey = info;
                }
                if (info.IsPrimaryKey && !info.KeepIdentity)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(info.VirtualField))
                {
                    continue;
                }
                object value = info.GetValue(obj);
                if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
                {
                    if (value == null)
                    {
                        continue;
                    }
                }
                value = ObjectConvert.SetNullValue(value, info.PropertyType);
                sql1 += string.Format("{0},", info.KeyWordName);
                sql2 += string.Format("@{0},", name);
                helper.AddParam(name, value);
            }
            sql1 = sql1.Substring(0, sql1.Length - 1);
            sql2 = sql2.Substring(0, sql2.Length - 1);
            sql += sql1 + ") values( " + sql2 + ") ; ";
            if (primaryKey.KeepIdentity)
            {
                sql += "SELECT " + primaryKey.GetValue(obj) + ";";
            }
            else
            {
                sql += "SELECT scope_identity() ;";
            }
            sql = SqlFormat(sql);
            return(Convert.ToInt32(helper.ExecScalar(sql)));
        }