开发者

Setting the database connection when using a TransactionScope

Does the database connection have to be set inside a TransactionScope?

Or can I set it in the ctor and then have instance methods create up a TransactionScope?

EDIT: e.g.

Public Sub New()
   Dim conn = new SqlConnection(...connection string)
Public Sub SomeClassMethod()
   using ts as new TransactionScope
      //conn has already been initialized
      /开发者_如何学Go/so, here you can set commands, ExecuteDataSet, etc.

vs

Public Sub New()
   //nothing here
Public Sub SomeClassMethod()    
   using ts as new TransactionScope
      conn = new SqlConnection(...connection string)
      set commands, ExecuteDataSet, etc.

the question is do you need to create the connection to the database after you've created a TransactionScope or can it be done before?


If you want you SqlConnection to be under transaction, than you need to create it under TransactionScope.

using(TransactionScope scope = new TransactionScope())
{
  SqlConnection x = new SqlConnestion("....");
  x.Open();
  ....your code... SQlCommands etc....
  x.Close();
  scope.Complete();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜