具有以下代码,如果我从方法中返回而未在事务对象上调用Commit()会发生什么情况?
var transaction = connection.BeginTransaction();
try
{
int val;
var myCommand = new SqlCommand(
@"SELECT ......",
connection, transaction);
using (var reader = myCommand.ExecuteReader())
{
if (reader.Read() && !reader.IsDBNull(0))
{
if (int.TryParse(reader.GetString(0), out val))
{
return val; // <--- this code executes
}
}
}
transaction.Commit() // <--- this code never executes
}
catch (Exception ex)
{
// Rollback the transaction.
}