I am working on an app that is inserting a lot of new objects (rows) and relationships between them. But at a certain point when an error occurs I want all the changes to the DataContext be disgarded and "thrown away". So that after an error I have a clean copy of the DataContext that matches the state of the database.
+1
A:
Edit
Alternatively, you could make use of the DataContext.Transaction, and use that to .Commit()
or .Rollback()
your changes.
ORIG
Just throw away that DataContext & Re-instantiate it.
Something like...
public void MyMethod(string connStr)
{
try
{
DataClasses1DataContext dc = new DataClasses1DataContext(connStr);
for (int i = 0; i < 100; i++)
{
try
{
//Do Stuff
//Insert Objects
dc.SubmitChanges();
}
catch (Exception ex) //So if it bombs in the loop, log your exception
{
Log(ex);
}
finally //Reinstantiate your DC
{
dc = new DataClasses1DataContext(connStr);
}
}
}
catch (Exception bigEx)
{
Log(bigEx);
}
}
Eoin Campbell
2009-05-15 08:15:57
+1
A:
You could also use the TransactionScope in a using statement. If you don't call .Complete() on the TransactionScope, all changes are rolled back when it is disposed (which happens when leaving the using statement).
Philippe
2009-05-15 10:23:15