Is there a difference SMO ServerConnection transac

2019-07-01 14:19发布

I am using SMO to create databases and tables on a SQL Server. I want to do so in a transaction. Are both of these methods of doing so valid and equivalent:

First method:

Server server;
//...
server.ConnectionContext.BeginTransaction();
//...
server.ConnectionContext.CommitTransaction();

Second method:

Server server;
// ...
SqlConnection conn = server.ConnectionContext.SqlConnectionObject;
SqlTransaction trans = conn.BeginTransaction();
// ...
trans.Commit();

1条回答
来,给爷笑一个
2楼-- · 2019-07-01 14:45

The two are equivalent. Using a SqlTransaction object allows you to place the transaction in an using scope:

using(SqlTransaction  trn = conn.BeginTransaction ())
{
 ...
 trn.Commit ();
}

This is a better pattern in presence of exceptions.

查看更多
登录 后发表回答