title: Enterprise Library Data Access Block Transaction Management Best Practice
body: <p>What is the best practice for processing a batched series of CRUD operations in a single transaction with the Enterprise Library Data Access Block that it won't be esclated to a distributed transaction?</p>
  
  <p><strong>Edit Full Source:</strong></p>
  
  <pre><code>public void BatchInsertEvents(IList&lt;EventItem&gt; events)
  {
      _dataAccessBase = new DataAccessBase("[dbo].[EventInsert]");
      int count = 0;
  
      try
      {
          using (var scope = 
                      new TransactionScope(TransactionScopeOption.RequiresNew))
          {
              foreach (var eventItem in events)
              {
                  _dataAccessBase.ClearParameters();
  
                  _dataAccessBase.AddInParameter("@time", 
                                              DbType.String, eventItem.Time);
                  ...more params
  
                  _dataAccessBase.ExecuteNonQuery();
                  count++;
              }
  
              scope.Complete();
          }
  }
  </code></pre>
  
  <p>My DataAccessBase is just a wrapper class for the Database object</p>
  
  <pre><code>public class DataAccessBase
  {
      private readonly DbCommand _command;
      private readonly Database _database;
  
      public DataAccessBase(string storedProcName) : this(null, storedProcName)
      {
      }
  
      public DataAccessBase(string connectionString, string storedProcName)
      {
          _database = string.IsNullOrEmpty(connectionString) ?
                       DatabaseFactory.CreateDatabase() : 
                       DatabaseFactory.CreateDatabase(connectionString);
          _command = _database.GetStoredProcCommand(storedProcName);
      }
  
      public void AddInParameter&lt;T&gt;(string parameterName, 
                                              DbType parameterType, T value)
      {
          _database.AddInParameter(_command, 
                                  parameterName, parameterType, value);
      }
  
  
      public void AddOutParameter&lt;T&gt;(string parameterName, 
                                  DbType parameterType, int parameterLength)
      {
          _database.AddOutParameter(_command, 
                                  parameterName, parameterType, parameterLength);
      }
  
      public void ClearParameters()
      {
          _command.Parameters.Clear();
      }
  
      public void ExecuteNonQuery()
      {
          _database.ExecuteNonQuery(_command);
      }
  }
  </code></pre>