SqlBulkCopyimports that span across all batches (and, optionally, across
other database statements) we need to use transactions. The following steps outline the
process of using a transaction with
- Create a SqlConnection to the destination database server.
- Open the connection.
- Create a
- Create the
SqlBulkCopyobject passing in the
SqlTransactionobject into the constructor.
- Perform the import - the call to
WriteToServer- within a
- If the operation completes, commit the transaction; if it fails, roll it back.
If you had other statements that needed to be performed under the umbrella of this
transaction you would execute them within the