开发者

Confirming a successful LINQ to SQL update

开发者 https://www.devze.com 2022-12-12 18:29 出处:网络
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?

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

精彩评论

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

关注公众号