Is there a difference SMO ServerConnection transaction methods versus using the SqlConnectionObject property?

筅森魡賤 提交于 2019-12-12 13:04:04

问题


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:


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.



来源:https://stackoverflow.com/questions/3057570/is-there-a-difference-smo-serverconnection-transaction-methods-versus-using-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!