33

I'm using SqlBulkCopy to restore tables from xml backups. One of the table backup is ~200MB large and has a lot of records.

I'm having error:

Timeout expired.
The timeout period elapsed prior to completion of the operation or the server
is not responding.
HasanG
  • 12,734
  • 29
  • 100
  • 154

3 Answers3

45

There are two ways to fix this error:

  • Increase Timeout by default it is 30 second and 0 means infinite.

       sqlBulkCopy.BulkCopyTimeout = {time in seconds}
    
  • Decrease BatchSize by default it try to insert all rows in one batch

        sqlBulkCopy.BatchSize  = {no of rows you want to insert at once}
    

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.batchsize.aspx http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.bulkcopytimeout.aspx

Abdul Saboor
  • 4,079
  • 2
  • 33
  • 25
  • 7
    See also: "What is the recommended batch size for SqlBulkCopy?" http://stackoverflow.com/questions/779690/what-is-the-recommended-batch-size-for-sqlbulkcopy – Mark Byers Jan 11 '16 at 12:16
  • 2
    Best answer is to set the default batch to use smaller chunks of data. Why? One really good reason is that if you have 45 million rows and you set the timeout to infinite, you will wait a really long time to finally run out of memory and crash the process which will result in zero rows inserted and a lot of frustration plus a really long test/dev cycle. I used a small batch of 100 rows without adjusting the timeout at all and hit three different (unrelated) errors. I was able to fail fast, make corrections and after each failure I had data in my table to test with. – Vance McCorkle May 19 '20 at 00:04
45

You probably need to increase the timeout. Try increasing the value of sqlBulkCopy.BulkCopyTimeout from the default which is 30 seconds.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
8

Change the CommandTimeout property of your SqlCommand object.

The Connect Timeout attribute of a connection string determines how long a SqlConnection Object runs before it stops attempting to connect to a server.

Also change the SqlBulkCopy.BulkCopyTimeout Property.

eriksv88
  • 3,482
  • 3
  • 31
  • 50