SqlTransaction to support multiple SqlConnections

Ian Best picture Ian Best · Feb 27, 2013 · Viewed 13.7k times · Source

So I have multiple SqlConnections that I want to all be used in one SqlTransaction. I know that I could use just one connection, but ultimately in each connection there is a good amount of newly declared and useless (after the connection is done) variables, and I need the program to stay as fast as possible.

I have each Connection in a using statement.

I am using the latest .NET and SQL Server 2008 R2.

Is it possible?

ALSO, I already viewed:

How to use a single SqlTransaction for multiple SqlConnections in .NET?

Nobody answered the question, the platforms are outdated.

Answer

Mike C. picture Mike C. · Feb 28, 2013

The question you linked has the answer that I would give you, TransactionScope.

The purpose of this is to have your connections automatically join an already existing transaction automatically.

using(System.Transacation.TransactionScope myScope = new TransactionScope()){
  //all of your sql connections and work in here

  //call this to commit, else everything will rollback
  myScope.Complete();
}

Look into transactionscope more here:

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx

If this doesn't answer your question, then I've thoroughly misunderstood it.