Ejemplo n.º 1
0
        public void Process()
        {
            _count = 0;
            bool aborted = false;

            try
            {
                OnRowsInserted(CopyEventType.Begin, "Initializing");

                if (_batchSize > 0)
                {
                    _bc.BatchSize   = _batchSize;
                    _bc.NotifyAfter = _batchSize;
                }

                _bc.RowsInserted += (s, e) =>
                {
                    _count  = e.Count;
                    e.Abort = OnRowsInserted(CopyEventType.Active);
                    if (e.Abort)
                    {
                        OnRowsInserted(CopyEventType.Error, "Aborted");
                        aborted = true;
                    }
                };
                _bc.BulkCopyTimeout = 35000;

                _bc.DestinationTableName = string.IsNullOrEmpty(Schema)
                                               ? Table
                                               : string.Format("[{0}].{1}", Schema, Table);

                DataTable st = _readerIn.GetSchemaTable();

                using (EnumerableDataReader reader = _readerIn)
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        string fieldName = reader.GetName(i);

                        _bc.AddColumnMappings(fieldName, fieldName, TypeConverter.ToDbType(st.Columns[i].DataType));
                    }

                    _bc.WriteToServer(reader);

                    reader.Close();

                    if (!aborted)
                    {
                        OnPostProcess();
                    }
                    OnComplete();
                }
            }
            catch (Exception ex)
            {
                OnRowsInserted(CopyEventType.Error, ex.Message);
            }
        }