Пример #1
0
        public void OnPostBulkInsert(BulkInsertEventArgs <T> e)
        {
            var handler = PostBulkInsert;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Пример #2
0
        /// <summary>
        /// Performs buffered bulk insert of enumerable items.
        /// </summary>
        /// <param name="items">The items to be inserted.</param>
        public void Insert(IEnumerable <T> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            // get columns that have a matching property
            var cols = _dt.Value.Columns.Cast <DataColumn>()
                       .Where(x => _props.Value.ContainsKey(x.ColumnName))
                       .Select(x => new { Column = x, Getter = _props.Value[x.ColumnName] })
                       .Where(x => x.Getter != null)
                       .ToArray();

            foreach (var buffer in Buffer(items, BufferSize).Select(x => x.ToArray()))
            {
                foreach (var item in buffer)
                {
                    var row = _dt.Value.NewRow();

                    foreach (var col in cols)
                    {
                        row[col.Column] = col.Getter(item) ?? DBNull.Value;
                    }

                    _dt.Value.Rows.Add(row);
                }

                var bulkInsertEventArgs = new BulkInsertEventArgs <T>(buffer, _dt.Value);
                OnPreBulkInsert(bulkInsertEventArgs);

                if (BulkCopyTimeout.HasValue)
                {
                    _sbc.BulkCopyTimeout = (int)BulkCopyTimeout.Value.TotalSeconds;
                }

                _sbc.WriteToServer(_dt.Value);

                OnPostBulkInsert(bulkInsertEventArgs);

                InsertedCount += _dt.Value.Rows.Count;
                _dt.Value.Clear();
            }
        }