Having the following code what would happen if I return from method without calling Commit() on the transaction object?
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.
}