You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The application connects to a Microsoft SQL Server database. You write the following code segment that executes two commands against the database within a transaction. (Line numbers are included for reference only.)
01using (SqlConnection connection = new SqlConnection(cnnStr)) {
02connection.Open();
03SqlTransaction sqlTran = connection.BeginTransaction();
04SqlCommand command = connection.CreateCommand();
05command.Transaction = sqlTran;
06try {
07command.CommandText = “INSERT INTO Production.ScrapReason(Name) VALUES(‘Wrong size’)”;
08command.ExecuteNonQuery();
09command.CommandText = “INSERT INTO Production.ScrapReason(Name) VALUES(‘Wrong color’)”;
10command.ExecuteNonQuery();
11
12}
You need to log error information if the transaction fails to commit or roll back.
Which code segment should you insert at line 11?
A.
sqlTran.Commit();
}
catch (Exception ex) {
sqlTran.Rollback();
Trace.WriteLine(ex.Message); }
B.
sqlTran.Commit();
}
catch (Exception ex) {
Trace.WriteLine(ex.Message);
try {
sqlTran.Rollback();
}
catch (Exception exRollback) {
Trace.WriteLine(exRollback.Message);
} } }
C.
catch (Exception ex){
Trace.WriteLine(ex.Message);
try{
sqlTran.Rollback();
} catch (Exception exRollback){
Trace.WriteLine(exRollback.Message);
}}
finaly { sqltran.commit( );}}
D.
catch (Exception ex) {
sqlTran.Rollback();
Trace.WriteLine(ex.Message);
}
finaly {
try {
sqltran.commit( );
} catch (Exception exRollback) {
Trace.WriteLine(excommit.Message); }}