transactions and rowstates

  • Thread starter Thread starter Erik Frey
  • Start date Start date
E

Erik Frey

Hi,

I rely on the SqlDataAdapter to do most of the work in my data layer.
The SqlDataAdapter uses DataRowState to decide what command to execute with
a given row, and then afterwards calls AcceptChanges on the row to render it
back to Unchanged, or remove it from the table.

My question is this:

I sometimes need to perform a number of DataAdapter operations, all in
one atomic go, so I use connection.BeginTransaction, then run a couple of
DataAdapter.Update() methods, then
transaction.Commit()/transaction.Rollback() depending on whether I encounter
an error.

If I -do- encounter an error, and rollback my transaction, is there a
reliable way to rollback my local datasets too, so that the datarows reflect
their original RowState before SQLDataAdapter bulldozed over them?

Thanks,

Erik
 
Hi Erik,

Erik Frey said:
Hi,

I rely on the SqlDataAdapter to do most of the work in my data layer.
The SqlDataAdapter uses DataRowState to decide what command to execute with
a given row, and then afterwards calls AcceptChanges on the row to render it
back to Unchanged, or remove it from the table.

My question is this:

I sometimes need to perform a number of DataAdapter operations, all in
one atomic go, so I use connection.BeginTransaction, then run a couple of
DataAdapter.Update() methods, then
transaction.Commit()/transaction.Rollback() depending on whether I encounter
an error.

If I -do- encounter an error, and rollback my transaction, is there a
reliable way to rollback my local datasets too, so that the datarows reflect
their original RowState before SQLDataAdapter bulldozed over them?

Yes, there is. A proper way would be to:
- extract changes from dataset using GetChanges() method.
- do Update on this new dataset
- if everything is fine, do a Merge of datasets
(originalDataset.Merge(changesDataset)
otherwise do nothing

Note that Merge won't work (without workarounds) with autoidentity fields.
 
Yes, there is. A proper way would be to:
- extract changes from dataset using GetChanges() method.
- do Update on this new dataset
- if everything is fine, do a Merge of datasets
(originalDataset.Merge(changesDataset)
otherwise do nothing

Note that Merge won't work (without workarounds) with autoidentity fields.

What an excellent suggestion. Thank you, Miha.

Erik
 
Back
Top