I am using LINQ to SQL. Is the following code开发者_如何转开发 proper if I want to notify the sender that the database was updated successfully or is there a better way?
try
{
dc.ModelA.InsertOnSubmit(modela);
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
The better way is to not catch the exception and let it propagate to the caller. By catching the exception you are removing all information about why the insert failed, making it very hard for anyone to debug and fix the problem. So you just need this:
dc.ModelA.InsertOnSubmit(modela);
dc.SubmitChanges();
Cleaner approach would be to wrap it in TransactionScope:
using (var scope = new TransactionScope())
{
dc.ModelA.InsertOnSubmit(modela);
dc.SubmitChanges();
scope.Complete();
}
精彩评论