SqlBulkCopy Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Problem

You execute the method WriteToServer, and the following error is thrown:

Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

// Oops! The specified bulkCopyTimeout value is to low!
bulkCopyTimeout = 1;

using (var connection = new SqlConnection(My.Config.ConnectionStrings.BulkOperations))
{
    connection.Open();

    using (var bulkCopy = new SqlBulkCopy(connection))
    {
        // SET BulkCopyTimeout value.
        bulkCopy.BulkCopyTimeout = bulkCopyTimeout;
        
        bulkCopy.DestinationTableName = destinationName;
        bulkCopy.WriteToServer(dt);
    }
}

Solution

Cause

  • The bulk copy take more time to execute than the BulkCopyTimeout specified.

Fix

  • INCREASE the BulkCopyTimeout value (default = 30s).
  • DECREASE the BatchSize value (default = unlimited).

The bulkCopyTimeout is by batch, not for the whole bulk operation.



Got any SqlBulkCopy Question?