I'm curious if following code would be considered safe?
using (SqlConnection cn = new SqlClient.SqlConnection(connectionString))
{
cn.Open();
using (SqlTransaction tr = cn.BeginTransaction())
{
try
{
if (!Data.DoSomething1(tr, p1, p2))
{
tr.Rollback();
return false;
}
foreach (ItemType item in Items)
{
if (!Data.DoSomething2(tr, p3, p4))
{
tr.Rollback();
return false;
}
}
tr.Commit();
return true;
}
catch (Exception myErr)
{
if (tr != null)
tr.Rollback();
throw myErr;
}
finally
{
if (cn != null)
{
cn.Close();
cn.Dispose();
}
}
}
}
I wanted to pass transaction 'tr' by ref but couldn't because it is within "using" construct. I would like to hear suggestions for better approach in similar situations.
Regards