"Unexpected existing transaction" ... Why this exception happens?
This happens because using the SqlBulkCopy
constructor without specifying a transaction will create its own transaction internally.
Avoid this by creating your transaction and then use it to create the SqlBulkCopy
. SqlBulkCopy
can be created with the transaction that you want to use, like this:
connection.Open();
using (var tran = connection.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, tran))
{