You use a local transaction to execute two SqlCommands, as shown here:

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction = connection.BeginTransaction("SampleTransaction");
try
{
command.CommandText = updateSQLCommandText;
command.ExecuteNonQuery();
command.CommandText = insertSQLCommandText;
command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}

When you test this code, you find that after adding the transaction code, the first command throws an exception. You must fix the problem and ensure transactional behavior.

What should you do?

 Posted by Rajkatie on 9/30/2012 | Category: ADO.NET Interview questions | Views: 2676 | Points: 40
Select from following answers:
  1. Set the Transaction property of SqlCommand to the transaction returned by BeginTransaction.
  2. Set an isolation level of ReadCommitted or higher explicitly when beginning the transaction.
  3. Begin the transaction before creating the command.
  4. All Above

Show Correct Answer


Source: MeasureUp.Com | | Alert Moderator 

Comments or Responses

Login to post response