Transaction in ADO.Net
The Code Snippet Demonstrates How Simple Transaction Works.
// Connection to your database using SQLConnection Object,Lets say SQLConnection Object name is sqlcon
SQLCommand sqlCom = sqlcon.CreateCommand();
SQLTransaction sqlTran;
// Start a local transaction
sqlTran = salcon.BeginTransaction();
/* Assign Both transaction object and connection to Command Object for pending local transaction */
sqlCom.Connection = sqlCon;
sqlCom.Transaction = sqlTran;
try
{
sqlCom.CommandText = "Insert or Update or Delete Command as per sql Syntax";
sqlCom.ExecuteNonQuery();
// Trying to Commit the Transaction
sqlTran.Commit();
MessageBox.Show("Records Are Written To Database....");
}
catch(Exception objException1)
{
MessageBox.Show("Commit Exception Occured :" + objException1.GetType());
// Trying to Rollback the Transaction
try
{
sqlTran.RollBack();
}
catch(Exception objException2)
{
MessageBox.Show("RollBack Exception Occured :" + objException2.GetType());
}
}
Good Info..
Keep Posting.