Faccio così:
public static bool SalvaCliente(DataRow cliente)
{
string sql = "INSERT ...";
try
{
return ExecuteNonQuery(sql);
}
catch
{
return false;
}
}
private static bool ExecuteNonQuery(string sql)
{
bool result = true;
try
{
TransactionOptions transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
transactionOptions.Timeout = new TimeSpan(0, 5, 0);
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
using (SqlConnection con = new SqlConnection(Costanti.CONNECTION_STRING))
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
try
{
if (cmd.ExecuteNonQuery() > 0)
{
con.Close();
result = true;
}
else
{
con.Close();
result = false;
}
}
catch (Exception ex)
{
con.Close();
con.Dispose();
result = false;
}
}
if (result) scope.Complete();
}//TransactionScope
}//Try
catch (Exception ex)
{
return false;
}
return result;
}