Thursday, 28 April 2011

SQL Transaction in asp.net

           SqlConnection MySqlConnection = new SqlConnection("Connection String");
            MySqlConnection.Open();
            SqlTransaction MyTransaction = MySqlConnection.BeginTransaction("MyTrans");
           
            SqlCommand MyCommand = new SqlCommand();
            MyCommand.Transaction = MyTransaction;
            MyCommand.Connection = MySqlConnection;
           
            try

            {
            MyCommand.CommandText ="Delete From Employe where Empid = 100";
            MyCommand.ExecuteNonQuery();
            MyCommand.CommandText ="Delete From Salary where Empid = 100";
            MyCommand.ExecuteNonQuery();       
            MyTransaction.Commit();
            }
            catch
            {
                MyTransaction.Rollback();
            }

finally
{
 MySqlConnection.Close();
}

No comments:

Post a Comment