Sql System.Transaction和System.Data.Common.DbTransaction之间有什么区别?
我在项目中使用Linq to实体(EF4.0)。我想在代码中使用事务。我在代码中找到了两种使用事务的方法。System.Transaction和System.Data.Common.DbTransaction之间有什么区别?哪一个性能更好 第一:Sql System.Transaction和System.Data.Common.DbTransaction之间有什么区别?,sql,entity-framework-4,transactions,linq-to-entities,transactionscope,Sql,Entity Framework 4,Transactions,Linq To Entities,Transactionscope,我在项目中使用Linq to实体(EF4.0)。我想在代码中使用事务。我在代码中找到了两种使用事务的方法。System.Transaction和System.Data.Common.DbTransaction之间有什么区别?哪一个性能更好 第一: using (testEntities ent = new testEntities()) { ent.Connection.Open(); using (Syst
using (testEntities ent = new testEntities())
{
ent.Connection.Open();
using (System.Data.Common.DbTransaction transaction = ent.Connection.BeginTransaction())
{
try
{
...
int er1 = ent.SaveChanges();
if (er1 > 0)
success = true;
else
success = false;
...
int er2 = ent.SaveChanges();
if (er2 > 0)
success = true;
else
success = false;
}
catch
{
success = false;
}
success = false;
if (success)
transaction.Commit();
else
transaction.Rollback();
}
}
第二:
using (testEntities ent = new testEntities())
{
ent.Connection.Open();
using (TransactionScope tscope= new TransactionScope())
{
...
int er1 = ent.SaveChanges();
...
int er2 = ent.SaveChanges();
tscope.Complete();
}
}
来自msdn
System.Transactions命名空间包含允许您编写自己的事务应用程序和资源管理器的类。具体来说,您可以创建和参与一个或多个参与者的事务(本地或分布式)
DbTransactions仅用于数据库事务。
TransactionScope为您提供自动事务注册功能。它将为您注册或创建新的事务。这可以包括数据库或分布式事务
查看以了解更多信息。来自msdn
System.Transactions命名空间包含允许您编写自己的事务应用程序和资源管理器的类。具体来说,您可以创建和参与一个或多个参与者的事务(本地或分布式)
DbTransactions仅用于数据库事务。
TransactionScope为您提供自动事务注册功能。它将为您注册或创建新的事务。这可以包括数据库或分布式事务
有关更多信息,请查看