user2621858
user2621858

Reputation: 121

Transaction implementation with Enterprise Library

I am using Microsoft.Practices.EnterpriseLibrary.Data for database related activities in my application. I have written some code where I am executing a deletion as well as updating some records using two ExecuteNonQuery. I want to put these in a single transaction. How I can implement that using Microsoft.Practices.EnterpriseLibrary.Data?

What modification is required in the following code to use a transaction?

Code is as following:

int iUpdate = 0;
Database db = DatabaseFactory.CreateDatabase(dbRegion);

try
{
    string sSQL = "DELETE FROM table1 WHERE Number = 1 ";
    db.ExecuteNonQuery(CommandType.Text, sSQL);

    string sqlCommand = "spInsertToTable";
    DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);

    iUpdate = db.ExecuteNonQuery(dbCommand);        
}
catch (Exception ex)
{
    throw;
}

Upvotes: 2

Views: 1820

Answers (1)

Sameer
Sameer

Reputation: 3183

You can use TransactionScope object for this.

Upvotes: 2

Related Questions