|
-
February 6th, 2005, 06:40 PM
#1
Please help with rollback
Hi everybody!
I've got a problem with a dataset rollbacking.
Database Server is Sql Server.
And I use my own form for data changing.
So, this is the code of data record removing:
Code:
dataSetType.someTableRow activeRow = dataSet.someTable.FindBysome_id(id);
activeRow.Delete();
DataRow[] changedRows = dataSet.someTable.Select("", "", DataViewRowState.Deleted);
if (changedRows == null)
{
throw new Exception("no changes were detected in dataset");
}
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
Transaction = Connection.BeginTransaction();
dataAdapter.DeleteCommand.Transaction = Transaction;
if (dataAdapter.Update(changedRows) == 0)
{
throw new Exception("no changes were detected in database after attempt to delete a record");
}
// here I throw my own exception for test
throw new Exception("");
dataSet.AcceptChanges();
Transaction.Commit();
MessageBox.Show("OK");
}
catch (Exception ex)
{
try
{
dataSet.RejectChanges();
}
catch {}
try
{
Transaction.Rollback();
}
catch {}
MessageBox.Show(ex.ToString());
}
finally
{
try
{
Transaction.Dispose();
}
catch {}
try
{
Connection.Close();
}
catch {}
}
the problem is than if any exceptions occure after update command like in the given example, then no data is passed to database, but the record I've delete is removed from dataset. If I comment the statement with Update command of DataAdapter, then everything works fine, I mean that record gets back after it was deleted. How to resolve that problem?
-
February 7th, 2005, 01:03 AM
#2
Re: Please help with rollback
Is all of this in the same function ?
If
throw new Exception("no changes were detected in database after attempt to delete a record");
executes then the rest of the function will not be executed.
When you throw an exception the function is terminated and execution returns to the caller, it is the caller who must handle the exception.
Last edited by Sahir; February 7th, 2005 at 01:06 AM.
-
February 7th, 2005, 12:09 PM
#3
Please help with rollback
I've forgotten
"try
{"
at the first statement.
I know all these things about exceptions and the way of their handling.
I don't think that it would be necessary to post new thread here if the problem is just in exceptions handling.
The point is that I cannot find "elegent" way to safely commit changes either in database or in dataset. "Safely" means that if an exception occures in any statement before AcceptChanges then dataset must be rolled back.
I tried to use GetChanges and then Merge to merge changes to existing dataset if everything went OK. But the problem is, that there may be another operations that might change the content of dataset. Because if dataset was already merged then it cannot be rolled back.
Then I tried Select, but this way isn't suitable either, because Update called for DataRow[] that's retrieved by Select, leads to directly sending changes to dataset.
Now I use I think not the best way.
After Update I need to perform a sequence of operations, which are the methods of a different classes. Each method makes changes and retrieves them in a form of DataSet (which actually containes changes). After this I can choose whether to use or not retrieved DataSet. In my case I chose to retrieve many of such datasets and merge them to the main DataSet in one statement. So if the problem occures at least in one operation the main DataSet won't be damaged.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|