sly_Chandan
sly_Chandan

Reputation: 3525

When would I use TransactionScope and Transaction in SQL

What is the difference between TransactionScope in dot net and Transaction in SQL

protected void Page_Load(object sender, EventArgs e)
        {            
            int i = 0;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            {
                i = 1;                
            }
            Response.Write(i.ToString());
        }

I get the value 1 displayed by Response.Write(). Why? scope.Complete wasnt executed. Hence the i value should rollback to 0;

Upvotes: 0

Views: 563

Answers (1)

IrishChieftain
IrishChieftain

Reputation: 15252

Basically the same thing; TransactionScope is the newer .NET version and the recommened version to use if you are coding on ASPNET 2.0 upwards.

TransactionScope Class

using (TransactionScope scope = new TransactionScope())
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure,
            "Update1");

        SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure,
            "Update2");
    }
    scope.Complete();
}

Upvotes: 1

Related Questions