Transaction with commit and rollback in .net using c# example

Transaction with commit and rollback in .net using c# example

public void TransactionIncSharp()
        {
            SqlTransaction objTrans = null;
            try
            {
                string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // get it from Web.config file 
                SqlConnection objConn = new SqlConnection(strConnString);
                objConn.Open();
                objTrans = objConn.BeginTransaction();
                
                string insert_str = "insert into YourTableName (ColName1,ColName2) values ('Test1','Test2')";
                SqlCommand objCmd = new SqlCommand(insert_str, objConn, objTrans);
                objCmd.ExecuteNonQuery();

                string insert_str1 = "insert into YourTableName1 (ColName1,ColName2) values ('Test','Transaction')";
                SqlCommand objCmd1 = new SqlCommand(insert_str1, objConn, objTrans);
                objCmd1.ExecuteNonQuery();
                objTrans.Commit();
               string message = "Successfully saved";
            }
            catch(Exception ex)
            {
               string message = ex.Message;
                objTrans.Rollback();
            }
        }

2020-01-17