Bug 22761 - SqlBulkCopy.GetColumnMetaData does not use user-provided transaction
Summary: SqlBulkCopy.GetColumnMetaData does not use user-provided transaction
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data (show other bugs)
Version: 3.8.0
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-09-08 17:55 UTC by Michael Nero
Modified: 2017-09-01 09:30 UTC (History)
2 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description Michael Nero 2014-09-08 17:55:10 UTC

SqlBulkCopy throws an exception when calling the WriteToServer(IDataReader) method if an instance is created using the constructor
   SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction).

The private method GetColumnMetaData correctly uses the connection provided in the ctor but not the transaction, which will throw an exception when the method executes the command it creates to get column metadata.

Steps to Reproduce:

1) Create an instance of SqlBulkCopy using the SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction) constructor. Ensure that the provided connection is open and the transaction is started.

2) Call the WriteToServer(IDataReader) method.

Actual Results:

A System.InvalidOperationException is thrown. The entire stack trace is below (my method calls were omitted from the stack trace)

System.InvalidOperationException: ExecuteReader requires a transaction if the command's connection is in a pending transaction.
  at System.Data.SqlClient.SqlCommand.ValidateCommand (System.String method, Boolean async) [0x00103] in C:\monosources\mono-3.6.0\mcs\class\System.Data\System.Data.SqlClient\SqlCommand.cs:778 
  at System.Data.SqlClient.SqlCommand.ExecuteReader (CommandBehavior behavior) [0x00000] in C:\monosources\mono-3.6.0\mcs\class\System.Data\System.Data.SqlClient\SqlCommand.cs:589 
  at System.Data.SqlClient.SqlCommand.ExecuteReader () [0x00000] in C:\monosources\mono-3.6.0\mcs\class\System.Data\System.Data.SqlClient\SqlCommand.cs:584 
  at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlCommand:ExecuteReader ()
  at System.Data.SqlClient.SqlBulkCopy.GetColumnMetaData () [0x00048] in C:\monosources\mono-3.6.0\mcs\class\System.Data\System.Data.SqlClient\SqlBulkCopy.cs:183 
  at System.Data.SqlClient.SqlBulkCopy.BulkCopyToServer (System.Data.DataTable table, DataRowState state) [0x00099] in C:\monosources\mono-3.6.0\mcs\class\System.Data\System.Data.SqlClient\SqlBulkCopy.cs:392 
  at System.Data.SqlClient.SqlBulkCopy.WriteToServer (IDataReader reader) [0x00015] in C:\monosources\mono-3.6.0\mcs\class\System.Data\System.Data.SqlClient\SqlBulkCopy.cs:590

Expected Results:

No exception is thrown. I can understand if the guidance in this case is to use TransactionScope instead of an external transaction. If that's the case, it should be documented until a work-around can be implemented.

Note You need to log in before you can comment on or make changes to this bug.