zzzprojects / Bulk-Operations

C# SQL Bulk Operations | High-performance C# bulk insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL, and SQLite.
https://bulk-operations.net
142 stars 36 forks source link

Bulk operation by enabling transaction #77

Closed rajkamal0610 closed 2 months ago

rajkamal0610 commented 2 months ago

I need sample code of how to use

**Begin transaction

Commit

Rollback**

While using bulkoperation insert/merge

I try below way

**Bulk.transaction = conn.begintransaction

Conn.transaction.commit ** The code above is not working for me. Throwing error like cannot access disposed object

JonathanMagnan commented 2 months ago

Hello @rajkamal0610 ,

Here is an online example how to use a transaction with the bulk operation library: https://dotnetfiddle.net/rFpG6G

var transaction = connection.BeginTransaction();

customers = GenerateCustomers(5);

// SAVE customers
using (var bulk = new BulkOperation<Customer>(connection))
{
    bulk.Transaction = transaction;
    bulk.DestinationTableName = "Customers";
    bulk.BulkMerge(customers);
}

transaction.Commit();

The Throwing error like cannot access disposed object is often associated when using an async method wrongly. The connection/transaction is already disposed before the code in the async method is executed. I'm not sure if that is currently your case as some code is missing for it.

Let me know if that answers your question correctly. If you need more assistance, please provide more code or an online example like mine.

Best Regards,

Jon

rajkamal0610 commented 2 months ago

Thanks for your quick response. Will update you with code sample

JonathanMagnan commented 2 months ago

Hello @rajkamal0610,

Since our last conversation, we haven't heard from you.

Let me know if you need further assistance.

Best regards,

Jon

rajkamal0610 commented 2 months ago

Thanks for the support. Code working fine