I am working with Visual Studio 2012 and MS SQL Server 2008 R2.
In my code I am using DbConnection and DbTransaction. This is my code:
DbConnection dbConnection = null;
DbTransaction dbTransaction = null;
try
{
dbConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection();
dbConnection.ConnectionString = connectionString;
dbConnection.Open();
dbTransaction = dbConnection.BeginTransaction();
// do my work using dbConnection and dbTransaction
dbTransaction.Commit();
}
catch (MyWorkFailedException mwfe)
{
dbTransaction.Rollback();
throw;
}
finally
{
if (dbConnection != null)
{
dbConnection.Close();
dbConnection.Dispose();
}
}
Is it possible that dbTransaction.Commit(); or dbTransaction.Rollback(); throws an exception?
If yes then how to handle it in my code? How c# programmers usually handles this situation? Or they dont handle this situation?
usingin your code?try finallysame as usingusingforDbConnection? If yes then it is just a preference and old habbit.usingis good pattern you should leverage.