Search articles in this blog and in my other related blogs.


Friday, July 4, 2008

Sql transaction with rollback and savepoint !

using (SqlConnection connection =
new SqlConnection(connectionString))
{
using (SqlCommand command =
connection.CreateCommand())
{
connection.Open();

SqlTransaction transaction =
connection.BeginTransaction();

command.Transaction = transaction;

try
{
command.CommandText = "Update...";
command.ExecuteNonQuery();

// Create a Savepoint
transaction.Save("FirstUpdate");

command.CommandText = "Insert...";
command.ExecuteNonQuery();

// Rollback to FirstUpdate
transaction.Rollback("FirstUpdate");

transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
}
}

source:http://davidhayden.com/blog/dave/archive/2005/10/15/2517.aspx

0 comments: