Question

I execute a stored procedure in my c# application, should I enclose it using TransactionScope as below ?

protected void rbl_SystemStatus_SelectedIndexChanged(object sender, EventArgs e)
{
    using (TransactionScope scope = new TransactionScope())
    {
        AdminData.myFunction();
        scope.Complete();
    }
}

myFunction() executes a stored procedure in my SQL Server Database, and the stored procedure content is like below

UPDATE Table A SET...
UPDATE Table B SET ...
DELETE FROM Table C WHERE ...
Was it helpful?

Solution

"should I" is subjective and contextual (it might not be enabled, for example; or you might use connection-based transactions; or you might not need any transaction); but it is certainly an option that is available to you. Whether it is "as below" depends on exactly where the connection is created/opened, as connections will only automatically enlist in a transaction-scope it they are initialized inside the transaction-scope. If the connection is created/opened inside myFunction, then yes it should work (caveat: unless the connection-string has Enlist explicitly set to false).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top