ado.nettransactionsatomicdataadapter

Atomicity of Data Adapter in ADO.NET


I am new to ADO.NET and learning it. I was wondering if Data Adapter in ADO.NET provides atomicity or ACID properties by itself when filling the Data Set and updating the Database, or do we have to use transaction explicitly to achieve this.

Let's say:

I want all the steps (can exclude first step if needed, as it will be a offline data which can be fetched in one go) to be done in one go, atomically, will I need a transaction ? If not how to achieve this?


Solution

  • I have checked the implementation of SqlDataAdapter with Reflector, and it looks to me as if it just executes the relevant commands (InsertCommand, UpdateCommand, DeleteCommand) one after another in sequence without imposing any sort of transaction around them. I suspect all other types of adapter written by Microsoft will work the same way.

    If you want atomic updates, you should create your own transaction, e.g.

    using(var scope = new TransactionScope()) {
        adapter.Update(dataSet);
        scope.Complete();
    }