开发者

Confirming a successful LINQ to SQL update

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜