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

Posted by  Tanuj Kumar
 2435  View(s)
Rate this:
Hi Expert, 

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();

Any help on above is really appreciated.
  1. Re: ADO.Net Transaction when Command's transaction is not set

    Hi Tanuj,

    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.