title: Memory Leak when executing SqlBulkCopy
body: <p>I am experiencing a bad memory leak which occurs in the following code:</p>
  
  <pre><code> public void BulkInsert(string tableName, IDataReader reader, String connectionString)
      {
          using (var connection = new SqlConnection(connectionString))
          {
              connection.Open();
              using (var bulkCopy = new SqlBulkCopy(connection))
              {
                  bulkCopy.DestinationTableName = tableName;
                  bulkCopy.BulkCopyTimeout = 900;
  
                  try
                  {
                      bulkCopy.WriteToServer(reader);
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                  }
                  finally
                  {
                      reader.Close();
                  }
              }
          }
      }
  </code></pre>
  
  <p>This code segment is executed thousands of times and as such it only takes a minute for an out of memory exception to occur. Ants reports that this is because the rows pointed to by the IDataReader are not being collected by the GC. However when I comment out the following line there is no leak which is how i've isolated the issue to this peice of code.</p>
  
  <pre><code>bulkCopy.WriteToServer(reader);
  </code></pre>
  
  <p>Has anyone a suggestion as to how to prevent the memory leak? </p>
  
  <p>Thank you in advance.</p>
  
  <p>Calling Code:</p>
  
  <pre><code> var reader = datatable.CreateDataReader();
   BulkInsert(tablename, reader, connectionString);
   reader.Dispose();
   datatable.Dispose();
  </code></pre>