Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have three procedure include three INSERT statements in three table in sql server.
How can I ROLLBACK all the procedure if error happened for any of them.

In other words, I want call procedure1, then some c# code, then call procedure2, then another c# code and then call procedure3. I just want to ROLLBACK all the procedures if error happened for any of them.
Please note that, I can not write a procedure with three INSERT statements.
Posted

Put your code inside TRY...CATCH block.
Inside TRY block put your Inserts and after the END; of the TRY Block put your COMMIT command.
Inside your CATCH put your Error message and your ROLLBACK command.
See this thread[^]. Another useful article here[^].

AFAIK, you only can use Rollback inside a PROCEDURE.
 
Share this answer
 
You can use TransactionScope to rollback your transactions.
Check these articles

All About TransactionScope[^]

http://www.c-sharpcorner.com/UploadFile/1326ef/transactionscope-in-C-Sharp/[^]

http://codingcramp.blogspot.com/2009/06/how-to-setup-and-use-transactionscope.html[^]


[EDIT]

static public int CreateTransactionScope(string connectString1)
       {

           int returnValue = 0;

           System.IO.StringWriter writer = new System.IO.StringWriter();

           try
           {
               using (TransactionScope scope = new TransactionScope())
               {
                   using (SqlConnection connection1 = new SqlConnection(connectString1))
                   {

                       connection1.Open();

                       returnValue = 0;
                       SqlCommand command1 = new SqlCommand("insertTab1", connection1);
                       returnValue = command1.ExecuteNonQuery();
                       writer.WriteLine("Rows to be affected by command1: {0}", returnValue);


                       SqlCommand command2 = new SqlCommand("insertTab2", connection1);
                       returnValue = command2.ExecuteNonQuery();
                       writer.WriteLine("Rows to be affected by command2: {0}", returnValue);

                       SqlCommand command3 = new SqlCommand("insertTab3", connection1);
                       returnValue = command3.ExecuteNonQuery();
                       writer.WriteLine("Rows to be affected by command3: {0}", returnValue);

                   }
                   scope.Complete();
                   MessageBox.Show("Success");
               }

           }
           catch (TransactionAbortedException ex)
           {
               writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
               MessageBox.Show(ex.Message);
           }
           catch (ApplicationException ex)
           {
               writer.WriteLine("ApplicationException Message: {0}", ex.Message);
               MessageBox.Show(ex.Message);
           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           Console.WriteLine(writer.ToString());

           return returnValue;
       }
 
Share this answer
 
v2
Comments
sahar11 19-Jul-14 8:30am    
I write this code in my project, but when happened error in procedure3, other procedures (1 and 2) executed and rollback do not happen. please see my code and help me.thanks


private void button1_Click(object sender, EventArgs e)
{
using (TransactionScope tr=new TransactionScope())
{

try
{
docommand("exec proc1"); // execute procedure1
docommand("exec proc2"); // execute procedure2
docommand("exec proc3"); // execute procedure3
tr.Complete();
tr.Dispose();
MessageBox.Show("Successful");

}

catch(TransactionException ex)
{
tr.Dispose();
MessageBox.Show(ex.ToString());
}
catch (Exception ex1)
{

MessageBox.Show(ex1.Message);

}
}
}
Dilan Shaminda 19-Jul-14 10:29am    
Hi sahar, i have written small peace of code using this MSDN article

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.complete(v=vs.110).aspx[^]

I have updated my solution for your reference.I don't know whether it is perfect solution for you.But it works fine for me.Please let me know if you have any doubts.Happy Coding :-)
sahar11 19-Jul-14 14:51pm    
Hi Hard_Rockz, thanks a lot for your attention.
I see your recommended article, and try to use this solution for my work but I have still problem whit rollback.
can I request of you to see my code and let me know my mistake.
I have a class for connect to database(access layer) and a function in this class (docommand) to execute my query. I create an instance of this class named al and then this code:


using (TransactionScope tr = new TransactionScope())
{
try
{

al.docommand("insert into st values (110,'ali',12,'red')");
al.docommand("insert into st values (111,'anna',12,'red')");
al.docommand("insert into st values (111,'sara',12,'red')");

tr.Complete();
tr.Dispose();
MessageBox.Show("Successful");

}
catch (TransactionException ex)
{
MessageBox.Show(ex.ToString());
}


catch
{
MessageBox.Show("error");
}

}


tertiary insert has an error and dont execute, but no rollback happen and two first insert has been executed and add data to my table(st).


I am sorry if my English Language is very bad.
Dilan Shaminda 19-Jul-14 15:19pm    
I have checked my posted code and it works fine.You can put your insert queries in insertTab1 , insertTab2 and insertTab3 . It rollback when error is happen to any of the table.Just try my code with your queries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900