I've got a problem, and all articles or examples iI found seem to not care about it. I
I want to do some database actions in a transaction. What iI want to do is very similar to most examples:
using (SqlConnection Conn = new SqlConnection(_ConnectionString))
{
try
{
Conn.Open();
SqlTransaction Trans = Conn.BeginTransaction();
using (SqlCommand Com = new SqlCommand(ComText, Conn))
{
/* DB work */
}
}
catch (Exception Ex)
{
Trans.Rollback();
return -1;
}
}
But the problem is, that the SqlTransaction TransSqlTransaction Trans is declared inside the trytry block. So it is not accessable in the catch()catch() block. Most examples just do Conn.Open()Conn.Open() and Conn.BeginTransaction()Conn.BeginTransaction() before the trytry block. But i, but I think thatsthat's a bit risky, since both can throw multiple exceptions.
Am I wrong, or do most people just ignore this risk? WhatsWhat's the best solution to be able to rollback, if an exception happens.
Thanks in advance, Marks?