Is it important to open sql connection in transaction

I created sqlconnection, CN1. Then this CN1 opens. There is a transaction later in the code. If I execute the sql command for this CN1 connection, is it inside a transaction?

The code is as follows:

SqlConnection cn1 = new SqlConnection(); cn1.Open(); //connection opened when there is no ambient transaction. ... using(TransactionScope scope = new TransactionScope()) { SqlCommand cmd; //a typical sql command. ... cmd.ExecuteNonQuery(); //Is this command within transaction? ... } 
+7
sqlconnection transactionscope
source share
2 answers

This is necessary to open a connection in TransactionScope to make sure that the connection is registered in the transaction.

This is in the comment just above connection.Open in this MSDN example.

+9
source share
  • No, the command is not executed in a transaction
  • Open the connection inside the scope or use the EnlistTransaction method of the SqlConnection instance. See my answer in another thread .
+5
source share

All Articles