Esempio n. 1
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);
        }