0

dot net中的TransactionScope和SQL中的Transaction有什么区别

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

我得到 Response.Write() 显示的值 1。为什么?scope.Complete 没有被执行。因此 i 值应该回滚到 0;

4

1 回答 1

1

基本上是一样的;TransactionScope 是较新的 .NET 版本,如果您在 ASPNET 2.0 及更高版本上进行编码,则推荐使用该版本。

TransactionScope 类

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();
}
于 2012-04-07T19:23:36.907 回答