Sunil
Sunil

Reputation: 21406

ADO.Net Transaction when Command's transaction is not set

I came across a sample on ADO.Net, where a transaction was being done without setting the command's transaction property as in code below.

Is this possible Or one needs to explicitly set the command's transaction property?

// Start a local transaction.
SqlTransaction sqlTran = connection.BeginTransaction();

// Enlist a command in the current transaction.
 SqlCommand command = connection.CreateCommand();
-----
-----
sqlTran.Commit()

Upvotes: 0

Views: 211

Answers (1)

jason
jason

Reputation: 241711

That should throw a runtime exception. That is, if you have an active transaction on a SqlConnection and do not assign a reference to the corresponding SqlTransaction to the SqlCommand.Transaction property and attempt to execute the command, you should get an exception.

In short, set the Transaction property explicitly when executing a command on a connection with an active transaction.

Upvotes: 2

Related Questions