OracleBulkCopy, manage rollback or commit after multiple WriteToServer()

587 Views Asked by At

I wanted to know if it is possible to do a Bulk Insert with a transaction via the OracleBulkCopy object in .Net Core.

The main problem is that I have to perform a double bulk insert operation on two different tables. But when I use the WriteToServer method I don't have the possibility to manage rollback or commit.

Example:

//pretending that these two DataTables are valued
DataTable dtOne;
DataTable dtTwo;

using(OracleConnection connection= new OracleConnection(//ConnectionString))
{
   using(OracleBulkCopy oracleBulkCopy= new OracleBulkCopy(connection)) 
   {

      //--- dtOne todo:
      //assign TableSchema and TableName to oracleBulkCopy
      //mapping on ColumnMappings of oracleBulkCopy
      
      oracleBulkCopy.WriteToServer(dtOne);

      //clear ColumnMappings

      //--- dtTwo todo:
      //assign TableSchema and TableName to oracleBulkCopy
      //mapping on ColumnMappings of oracleBulkCopy

      oracleBulkCopy.WriteToServer(dtOne);

      //ToDo
   }
}

What happens is not what I expect since, once the WriteToServer(dtOne) method is executed on the first DataTable, all the records of dtOne are committed. My goal is to wait for the second WriteToServer(dtTwo) to run as well and then commit all the changes.

In fact, if something goes wrong on WriteToServer(dtTwo), I can't perform a total rollback.

A lot of similar questions were asked a few years ago (4/5 years ago), I don't know if anything has changed in the meantime

Any suggestions are welcome, thanks in advance

0

There are 0 best solutions below