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;
答案 0 :(得分:1)
基本上是一样的; TransactionScope是较新的.NET版本,如果您在ASPNET 2.0上编码,则使用推荐的版本。
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();
}