示例#1
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="helper"></param>
        /// <returns></returns>
        public override int InsertObject(IModel obj, CoreHelper.DBHelper helper)
        {
            Type   type     = obj.GetType();
            string table    = TypeCache.GetTableName(type);
            var    typeArry = TypeCache.GetProperties(type, true).Values;
            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)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(info.VirtualField))
                {
                    continue;
                }
                object value = info.GetValue(obj);
                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 + ") ; SELECT scope_identity() ;";
            sql  = SqlFormat(sql);
            return(Convert.ToInt32(helper.ExecScalar(sql)));
        }
示例#2
0
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <typeparam name="TItem"></typeparam>
        /// <param name="helper"></param>
        /// <param name="details"></param>
        /// <param name="keepIdentity"></param>
        public override void BatchInsert <TItem>(CoreHelper.DBHelper helper, List <TItem> details, bool keepIdentity = false)
        {
            string    table     = TypeCache.GetTableName(typeof(TItem));
            string    sql       = GetSelectTop("*", " from " + table + " where 1=0", "", 1);
            DataTable tempTable = helper.ExecDataTable(sql);
            var       typeArry  = TypeCache.GetProperties(typeof(TItem), true).Values;

            foreach (TItem item in details)
            {
                DataRow dr = tempTable.NewRow();
                foreach (Attribute.FieldAttribute info in typeArry)
                {
                    string name  = info.Name;
                    object value = info.GetValue(item);
                    if (!keepIdentity)
                    {
                        if (info.IsPrimaryKey)
                        {
                            continue;
                        }
                    }
                    if (!string.IsNullOrEmpty(info.VirtualField))
                    {
                        continue;
                    }
                    var value2 = ObjectConvert.SetNullValue(value, info.PropertyType);
                    dr[name] = value2;
                }
                tempTable.Rows.Add(dr);
            }
            helper.InsertFromDataTable(tempTable, table, keepIdentity);
        }
示例#3
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)));
        }
示例#4
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="helper"></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;
            string sql      = string.Format("insert into {0}(", table);
            string sql1     = "";
            string sql2     = "";

            string sequenceName = string.Format("{0}_sequence", table);
            var    sqlGetIndex  = string.Format("select {0}.nextval from dual", sequenceName);//oracle不能同时执行多条语句
            int    id           = Convert.ToInt32(helper.ExecScalar(sqlGetIndex));

            foreach (Attribute.FieldAttribute info in typeArry)
            {
                string name = info.Name;
                if (info.IsPrimaryKey && !info.KeepIdentity)
                {
                    //continue;//手动插入ID
                }
                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},", info.KeyWordName);
                helper.AddParam(info.KeyWordName, value);
            }
            sql1 = sql1.Substring(0, sql1.Length - 1);
            sql2 = sql2.Substring(0, sql2.Length - 1);
            sql += sql1 + ") values( " + sql2 + ")";
            sql  = SqlFormat(sql);
            var primaryKey = TypeCache.GetTable(obj.GetType()).PrimaryKey;

            helper.SetParam(primaryKey.Name, id);
            helper.Execute(sql);
            //var helper2 = helper as CoreHelper.OracleHelper;
            //int id = helper2.Insert(sql,sequenceName);
            return(id);
        }
示例#5
0
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="details"></param>
        /// <param name="keepIdentity"></param>
        public override void BatchInsert(System.Collections.IList details, bool keepIdentity = false)
        {
            if (details.Count == 0)
            {
                return;
            }
            var       type      = details[0].GetType();
            string    table     = TypeCache.GetTableName(type, dbContext);
            string    sql       = GetSelectTop("*", " from " + table + " where 1=0", "", 1);
            DataTable tempTable = helper.ExecDataTable(sql);
            var       typeArry  = TypeCache.GetProperties(type, true).Values;

            foreach (var item in details)
            {
                DataRow dr = tempTable.NewRow();
                foreach (Attribute.FieldAttribute info in typeArry)
                {
                    string name  = info.Name;
                    object value = info.GetValue(item);
                    if (!keepIdentity)
                    {
                        if (info.IsPrimaryKey)
                        {
                            continue;
                        }
                    }
                    if (!string.IsNullOrEmpty(info.VirtualField))
                    {
                        continue;
                    }
                    var value2 = ObjectConvert.SetNullValue(value, info.PropertyType);
                    if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
                    {
                        if (value2 == null)
                        {
                            continue;
                        }
                    }
                    dr[name] = value2;
                }
                tempTable.Rows.Add(dr);
            }
            helper.InsertFromDataTable(tempTable, table, keepIdentity);
        }