ARTICLE

Executing multiple stored procedures within a single transaction context from ADO.Net

Posted by Santhosh Veeraraman Articles | ADO.NET in C# March 20, 2008
This article discusses how we can execute multiple stored procedures in ADO.NET with different parameter values in a single Transaction context.
Reader Level:
Download Files:
 

Introduction

This article can be treated as an extension of my earlier post Generic Data Layer


http://www.csharpcorner.com/UploadFile/vmsanthosh.chn/209062007040459AM/2.aspx?ArticleID=96a34245-427e-4e57-adb0-f75ce28d5b1d
 
 
This aims to explain how we can execute different stored procedures in ADO.Net with different parameter values with in a single Transaction context, so that if anything gone wrong in any of the stored procedure, all others should be roll backed or else committed.
 
Why do I need this? "Practical approach"
 
Very often, we might have encountered various scenarios where we need to insert records in a batch.  This can go in case of a finance application where the data is tightly coupled and inter depended as well as inserting records of a grid-view (let us keep aside datasets for a while, because there is no transactions involved) in click of a button.  The handling of data in case of finance application is more complex where the dependency is high.  For example General Ledgers has to be available only when the journal entries are successful.
 
Of course we can handle this very well, with in the stored procedure itself as we have transactions in any RDBMS you name. But what if we need to combine multiple stored procedures together and run it with in a single transaction boundary, especially when I am forced to do it only through ADO.Net?  (Again please keep aside DTC for a while because I feel it is bit pain for initial configurations)

Solution
 
I tried to get a work around fix and my objective was to reduce the burden of developer as much as possible. Something needs to be done where things can be handled very effectively with minimum lines of code. And here is what I tried.
 
To get this code up, you need to have Microsoft Applications Data block that can be downloaded either from Microsoft or from my earlier blog
http://www.c-sharpcorner.com/UploadFile/vmsanthosh.chn/209062007040459AM/2.aspx?ArticleID=96a34245-427e-4e57-adb0-f75ce28d5b1d

Fine, But how do I use it?

Usage of this wrapper class is quite simple.  

DAC DC = new DAC();

DC.StoredProcedure = "nProc_InsertOrder";

DC.Params.Add("@OrderId", SqlDbType.VarChar, "Order1" );

DC.Params.Add("@CustomerName", SqlDbType.VarChar, "test");

DAC.Commands.Add(DC);
 

DC = new DAC();

DC.StoredProcedure = "nProc_InsertOrderLineItems";

DC.Params.Add("@OrderId", SqlDbType.VarChar, "Order1" );

DC.Params.Add("@OrderLineId", SqlDbType.VarChar, "A1");

DAC.Commands.Add(DC);
 

DC = new DAC();

DC.StoredProcedure = "nProc_InsertOrderLineItems";

DC.Params.Add("@OrderId", SqlDbType.VarChar, "Order1" );

DC.Params.Add("@OrderLineId", SqlDbType.VarChar, "A21erLineId");

DAC.Commands.Add(DC);
 

DC = new DAC();

DC.StoredProcedure = "nProc_CreateBill";

DC.Params.Add("@BillDate", SqlDbType.DateTime, DateTime.Now);

DC.Params.Add("@BillId", SqlDbType.VarChar, "Bill1");

DAC.Commands.Add(DC);
DAC.ExecuteBatch();
 
If the order insertion is failed, the bill should not be created. Similarly, if the line items are failed, then the order should not be created. We are achieving this in just a few lines of code through ADO.Net.
 
In this example, till we call ExecuteBatch, we are not actually inserting the records but preparing the object for making batch updations.
 
Hope this would be of helpful for you for your projects. Mark your comments and suggestions.
 
Happy Coding.

Login to add your contents and source code to this article
post comment
     

I get this error. The type'Sys.DAC.DAC' in 'c:\users\ragul\documents\visual studio 2010\Projects\TestingNewCodes\DAC.cs' conflicts with the namespace 'System.DAC.DAC' in 'c\users\ragul\documents\visual studio 2010\TestingNewCodes\TestingNewCodes\DAC.cs

Posted by Ragul Vasudevan May 17, 2013

the function ExecuteBatch() icannot found it inside dac class which i download it if it ib dac_new.zip please re upload it becuase i cannot download it the link is damage

Posted by ahmed mostafa May 30, 2012

This is what I was looking for .. can you please upload new file as DAC file also missing UTILITY class

Thanks

Posted by Rakesh Kumar Aug 04, 2010

please, upload DAC_new 

Posted by seal cjul Aug 07, 2009

One small correction in the DAC code: Inside ExecuteBatch try block, it is supposed to be Trans.Commit() and not Trans.Rollback(). And in Finally block, add line Commands.Clear(); just after Parameters.MyParameters.Clear();. Thats it. Please make the correction in your code if anybody has downloaded it by chance...I will be uploading the modified code again !

Posted by Santhosh Veeraraman Mar 19, 2008
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts