ADO.NET TRANSACTION WHEN COMMAND'S TRANSACTION IS NOT SET

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
 1426  View(s)
Ratings:
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();
-----
-----
sqlTran.Commit()

Any help on above is really appreciated.
  1. Sumit Kesarwani

    Post:378

    Points:2694
    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!