BulkCopy() public static method

public static BulkCopy ( DataTable dt, ObjectName destinationTable, SqlBulkCopyOptions options, int timeout ) : void
dt System.Data.DataTable
destinationTable ObjectName
options SqlBulkCopyOptions
timeout int
return void
コード例 #1
0
ファイル: BulkInserter.cs プロジェクト: doganc/framework
        public static int BulkInsertView <T>(this IEnumerable <T> entities,
                                             SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
                                             int?timeout    = null,
                                             string?message = null)
            where T : IView
        {
            using (HeavyProfiler.Log(nameof(BulkInsertView), () => typeof(T).Name))
            {
                if (message != null)
                {
                    return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering {entities.Count()} {typeof(T).TypeName()}" : message,
                                                () => BulkInsertView(entities, copyOptions, timeout, message: null)));
                }

                if (copyOptions.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
                {
                    throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
                }

                var t = Schema.Current.View <T>();

                var list = entities.ToList();

                bool disableIdentityBehaviour = copyOptions.HasFlag(SqlBulkCopyOptions.KeepIdentity);

                var       columns = t.Columns.Values.ToList();
                DataTable dt      = new DataTable();
                foreach (var c in columns)
                {
                    dt.Columns.Add(new DataColumn(c.Name, ConvertType(c.Type)));
                }

                foreach (var e in entities)
                {
                    dt.Rows.Add(t.BulkInsertDataRow(e));
                }

                using (Transaction tr = new Transaction())
                {
                    Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);

                    Executor.BulkCopy(dt, columns, t.Name, copyOptions, timeout);

                    return(tr.Commit(list.Count));
                }
            }
        }
コード例 #2
0
        public static int BulkInsertMListTable <E, V>(
            this IEnumerable <MListElement <E, V> > mlistElements,
            Expression <Func <E, MList <V> > > mListProperty,
            SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
            int?timeout    = null,
            string message = null)
            where E : Entity
        {
            using (HeavyProfiler.Log(nameof(BulkInsertMListTable), () => $"{mListProperty} ({typeof(MListElement<E, V>).TypeName()})"))
            {
                if (message != null)
                {
                    return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering MList<{ typeof(V).TypeName()}> in { typeof(E).TypeName()}" : message,
                                                () => BulkInsertMListTable(mlistElements, mListProperty, copyOptions, timeout, message: null)));
                }

                if (copyOptions.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
                {
                    throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
                }

                DataTable dt = new DataTable();
                var       t  = ((FieldMList)Schema.Current.Field(mListProperty)).TableMList;
                foreach (var c in t.Columns.Values.Where(c => !(c is SystemVersionedInfo.Column) && !c.IdentityBehaviour))
                {
                    dt.Columns.Add(new DataColumn(c.Name, c.Type.UnNullify()));
                }

                var list = mlistElements.ToList();

                foreach (var e in list)
                {
                    dt.Rows.Add(t.BulkInsertDataRow(e.Parent, e.Element, e.Order));
                }

                using (Transaction tr = new Transaction())
                {
                    Schema.Current.OnPreBulkInsert(typeof(E), inMListTable: true);

                    Executor.BulkCopy(dt, t.Name, copyOptions, timeout);

                    return(tr.Commit(list.Count));
                }
            }
        }
コード例 #3
0
        public static int BulkInsert <T>(IEnumerable <T> entities,
                                         SqlBulkCopyOptions options = SqlBulkCopyOptions.Default, bool validateFirst = false, int?timeout = null, string message = null)
            where T : Entity
        {
            if (message != null)
            {
                return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering { typeof(T).TypeName()}" : message,
                                            () => BulkInsert(entities, options, validateFirst, timeout, message: null)));
            }

            if (options.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
            {
                throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
            }

            var list = entities.ToList();

            if (validateFirst)
            {
                Validate <T>(list);
            }

            var t = Schema.Current.Table <T>();

            DataTable dt = CreateDataTable <T>(list, t);

            using (Transaction tr = new Transaction())
            {
                Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);

                Executor.BulkCopy(dt, t.Name, options, timeout);

                foreach (var item in list)
                {
                    item.SetNotModified();
                }

                return(tr.Commit(list.Count));
            }
        }
コード例 #4
0
        public static int BulkInsertDisableIdentity <T>(IEnumerable <T> entities,
                                                        SqlBulkCopyOptions options = SqlBulkCopyOptions.Default, bool validateFirst = false, int?timeout = null)
            where T : Entity
        {
            options |= SqlBulkCopyOptions.KeepIdentity;

            if (options.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
            {
                throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
            }

            var list = entities.ToList();

            if (validateFirst)
            {
                Validate <T>(list);
            }

            var t = Schema.Current.Table <T>();

            using (Transaction tr = new Transaction())
            {
                Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);

                using (DisableIdentity <T>())
                {
                    DataTable dt = CreateDataTable <T>(list, t);

                    Executor.BulkCopy(dt, t.Name, options, timeout);

                    foreach (var item in list)
                    {
                        item.SetNotModified();
                    }

                    return(tr.Commit(list.Count));
                }
            }
        }
コード例 #5
0
ファイル: BulkInserter.cs プロジェクト: doganc/framework
        public static int BulkInsertMListTable <E, V>(
            this IEnumerable <MListElement <E, V> > mlistElements,
            Expression <Func <E, MList <V> > > mListProperty,
            SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
            int?timeout            = null,
            bool?updateParentTicks = null,  /*Needed for concurrency and Temporal tables*/
            string?message         = null)
            where E : Entity
        {
            using (HeavyProfiler.Log(nameof(BulkInsertMListTable), () => $"{mListProperty} ({typeof(MListElement<E, V>).TypeName()})"))
            {
                if (message != null)
                {
                    return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering MList<{ typeof(V).TypeName()}> in { typeof(E).TypeName()}" : message,
                                                () => BulkInsertMListTable(mlistElements, mListProperty, copyOptions, timeout, updateParentTicks, message: null)));
                }

                if (copyOptions.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
                {
                    throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
                }

                var mlistTable = ((FieldMList)Schema.Current.Field(mListProperty)).TableMList;

                if (updateParentTicks == null)
                {
                    updateParentTicks = mlistTable.PrimaryKey.Type != typeof(Guid) && mlistTable.BackReference.ReferenceTable.Ticks != null;
                }

                var maxRowId = updateParentTicks.Value ? Database.MListQuery(mListProperty).Max(a => (PrimaryKey?)a.RowId) : null;

                DataTable dt      = new DataTable();
                var       columns = mlistTable.Columns.Values.Where(c => !(c is SystemVersionedInfo.SqlServerPeriodColumn) && !c.IdentityBehaviour).ToList();
                foreach (var c in columns)
                {
                    dt.Columns.Add(new DataColumn(c.Name, ConvertType(c.Type)));
                }

                var list = mlistElements.ToList();

                foreach (var e in list)
                {
                    dt.Rows.Add(mlistTable.BulkInsertDataRow(e.Parent, e.Element, e.Order));
                }

                using (Transaction tr = new Transaction())
                {
                    Schema.Current.OnPreBulkInsert(typeof(E), inMListTable: true);

                    Executor.BulkCopy(dt, columns, mlistTable.Name, copyOptions, timeout);

                    var result = list.Count;

                    if (updateParentTicks.Value)
                    {
                        Database.MListQuery(mListProperty)
                        .Where(a => maxRowId == null || a.RowId > maxRowId)
                        .Select(a => a.Parent)
                        .UnsafeUpdate()
                        .Set(e => e.Ticks, a => TimeZoneManager.Now.Ticks)
                        .Execute();
                    }

                    return(tr.Commit(result));
                }
            }
        }
コード例 #6
0
ファイル: BulkInserter.cs プロジェクト: doganc/framework
        public static int BulkInsertTable <T>(IEnumerable <T> entities,
                                              SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
                                              bool preSaving       = true,
                                              bool validateFirst   = true,
                                              bool disableIdentity = false,
                                              int?timeout          = null,
                                              string?message       = null)
            where T : Entity
        {
            using (HeavyProfiler.Log(nameof(BulkInsertTable), () => typeof(T).TypeName()))
            {
                if (message != null)
                {
                    return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering {entities.Count()} {typeof(T).TypeName()}" : message,
                                                () => BulkInsertTable(entities, copyOptions, preSaving, validateFirst, disableIdentity, timeout, message: null)));
                }

                if (disableIdentity)
                {
                    copyOptions |= SqlBulkCopyOptions.KeepIdentity;
                }

                if (copyOptions.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
                {
                    throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
                }

                var list = entities.ToList();

                if (preSaving)
                {
                    Saver.PreSaving(() => GraphExplorer.FromRoots(list));
                }

                if (validateFirst)
                {
                    Validate <T>(list);
                }

                var  t = Schema.Current.Table <T>();
                bool disableIdentityBehaviour = copyOptions.HasFlag(SqlBulkCopyOptions.KeepIdentity);

                DataTable dt      = new DataTable();
                var       columns = t.Columns.Values.Where(c => !(c is SystemVersionedInfo.SqlServerPeriodColumn) && (disableIdentityBehaviour || !c.IdentityBehaviour)).ToList();
                foreach (var c in columns)
                {
                    dt.Columns.Add(new DataColumn(c.Name, ConvertType(c.Type)));
                }

                using (disableIdentityBehaviour ? Administrator.DisableIdentity(t, behaviourOnly: true) : null)
                {
                    foreach (var e in list)
                    {
                        if (!e.IsNew)
                        {
                            throw new InvalidOperationException("Entites should be new");
                        }
                        t.SetToStrField(e);
                        dt.Rows.Add(t.BulkInsertDataRow(e));
                    }
                }

                using (Transaction tr = new Transaction())
                {
                    Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);

                    Executor.BulkCopy(dt, columns, t.Name, copyOptions, timeout);

                    foreach (var item in list)
                    {
                        item.SetNotModified();
                    }

                    return(tr.Commit(list.Count));
                }
            }
        }
コード例 #7
0
        public static int BulkInsertTable <T>(IEnumerable <T> entities,
                                              SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
                                              bool preSaving       = false,
                                              bool validateFirst   = false,
                                              bool disableIdentity = false,
                                              int?timeout          = null,
                                              string message       = null)
            where T : Entity
        {
            if (message != null)
            {
                return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering {entities.Count()} {typeof(T).TypeName()}" : message,
                                            () => BulkInsertTable(entities, copyOptions, preSaving, validateFirst, disableIdentity, timeout, message: null)));
            }

            if (disableIdentity)
            {
                copyOptions |= SqlBulkCopyOptions.KeepIdentity;
            }

            if (copyOptions.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
            {
                throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
            }

            var list = entities.ToList();

            if (preSaving)
            {
                Schema schema = Schema.Current;
                GraphExplorer.PreSaving(() => GraphExplorer.FromRoots(list), (Modifiable m, ref bool graphModified) =>
                {
                    if (m is ModifiableEntity me)
                    {
                        me.SetTemporalErrors(null);
                    }

                    m.PreSaving(ref graphModified);

                    if (m is Entity ident)
                    {
                        schema.OnPreSaving(ident, ref graphModified);
                    }
                });
            }

            if (validateFirst)
            {
                Validate <T>(list);
            }

            var  t = Schema.Current.Table <T>();
            bool disableIdentityBehaviour = copyOptions.HasFlag(SqlBulkCopyOptions.KeepIdentity);
            bool oldIdentityBehaviour     = t.IdentityBehaviour;

            DataTable dt = new DataTable();

            foreach (var c in disableIdentityBehaviour ? t.Columns.Values : t.Columns.Values.Where(c => !c.IdentityBehaviour))
            {
                dt.Columns.Add(new DataColumn(c.Name, c.Type.UnNullify()));
            }

            if (disableIdentityBehaviour)
            {
                t.IdentityBehaviour = false;
            }
            foreach (var e in entities)
            {
                if (!e.IsNew)
                {
                    throw new InvalidOperationException("Entites should be new");
                }
                t.SetToStrField(e);
                dt.Rows.Add(t.BulkInsertDataRow(e));
            }
            if (disableIdentityBehaviour)
            {
                t.IdentityBehaviour = oldIdentityBehaviour;
            }

            using (Transaction tr = new Transaction())
            {
                Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);

                Executor.BulkCopy(dt, t.Name, copyOptions, timeout);

                foreach (var item in list)
                {
                    item.SetNotModified();
                }

                return(tr.Commit(list.Count));
            }
        }