You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to develop an application
that connects to a Microsoft SQL Server 2008 database. The application includes a SqlConnection
named conn and a SqlCommand named cmd. You need to create a transaction so that database
changes will be reverted in the event that an exception is thrown. Which code segment should you
use?
A.
var transaction = conn.BeginTransaction();
cmd.Transaction = transaction;
try
{
…
transaction.Commit();
}
catch
{
transaction.Rollback();
}
B.
var transaction = conn.BeginTransaction();
cmd.Transaction = transaction;
try
{
…
transaction.Commit();
}
catch
{
transaction.Dispose();
}
C.
var transaction = conn.BeginTransaction();
cmd.Transaction = transaction;
try
{
…
}
catch
{
transaction.Commit();
}
D.
var transaction = conn.BeginTransaction();
cmd.Transaction = transaction;
try
{
…
transaction.Rollback();
}
catch
{
transaction.Dispose ();
}