Ejemplo n.º 1
0
        public void Start()
        {
            if (didStart)
            {
                throw new Exception("DbBulkCopierX can be started only once.");
            }

            feedableDataReader = new FeedableDataReader();
            foreach (string nextColumn in ColumnNames)
            {
                feedableDataReader.Columns.Add(nextColumn);
            }

            Log(string.Format("Opening connection <{0}>", ConnectionString));
            bulkCopy = BulkCopy.GetBulkCopy(ConnectionType, ConnectionString);
            Log("Opened connection.");
            bulkCopy.DestinationTableName = TableName;
            foreach (string nextColumn in ColumnNames)
            {
                bulkCopy.AddColumnMapping(nextColumn, nextColumn);
            }
            Log("Setting timeout value to " + Timeout + ".");
            bulkCopy.Timeout = Timeout;
            Log("Setting batch value to " + BatchSize + ".");
            bulkCopy.BatchSize = BatchSize;

            bulkCopyTask = Task.Factory.StartNew(() =>
            {
                try
                {
                    bulkCopy.WriteToServer(feedableDataReader);
                }
                catch (Exception exc)
                {
                    Log("Exception occurred: " + exc.Message);
                    bulkCopyException = exc;
                    // Unblock possible thread that is running AcceptRow().
                    feedableDataReader.UnblockRowAccept();
                    // Unblock future call to TellNoMoreRows().
                    feedableDataReader.UnblockRowAccept();
                    bulkCopy.Dispose();
                    bulkCopy = null;
                    throw;
                }
            });
            didStart = true;
        }
Ejemplo n.º 2
0
 public void Dispose()
 {
     if (feedableDataReader != null)
     {
         Log("Waiting for bulk copy task to end...");
         feedableDataReader.TellNoMoreRows();
         if (bulkCopyTask != null)
         {
             bulkCopyTask.Wait();
             bulkCopyTask = null;
         }
         if (bulkCopy != null)
         {
             bulkCopy.Dispose();
             bulkCopy = null;
         }
         Log("Closing reader and connection...");
         feedableDataReader.Close();
         feedableDataReader = null;
         Log("Closed reader and connection.");
     }
 }