Click here to Skip to main content
15,887,256 members
Articles / Programming Languages / C#

Run any stored procedure using that C# code

Rate me:
Please Sign up or sign in to vote.
1.30/5 (26 votes)
22 Apr 2009LGPL32 min read 80.3K   1.2K   39   8
That code can run any type of stored procedure.
Download SP_v1.zip - 2.12 KB
Download and Read details latest (v2) code from Here

Introduction  

That code will help to developer to develop code to run Stored Procedure easily from .Net (if you built it .dll file). Any one can write just few lines code to use it. If any one get any bug then please inform me at sumanbiswas@aol.in. To know more about such programming please visit to my blog http://socketprogramming.blogspot.com.

Background

When I try to run stored procedure from C# then I see that to run a single stored procedure need to write few line of code also to pass parameter need to write more line of code. So i decide to write some code by using stored procedure can run very fast. And now my code can run a Stored procedure by invoke just one function, and per parameter just one line code.

Use this code to run stored procedure<o:p> 

You can run any stored procedure by invoking two static methods. These are -<o:p>

1.      spArgumentsCollection - this is a static method of SP class, used to pass arguments to stored procedure. It has four arguments, for example, SP.spArgumentsCollection(arLst, "@nextName", "Suman Biswas", "varchar"); here ‘SP’ is the class name, ‘arLst’ is an ArrayList, “@nextName” is arguments name in Stored Procedure, “Suman Biswas” is the value of argument and “varchar” is argument type (in version 2, this has updated to enum type). <o:p>

2.      RunStoredProcedure- this is used to run stored procedure. If stored procedure has argument then need to set argument(s) in ArrayList then have to call that method. For e.g. SP.RunStoredProcedure(cnnStr, "UpdateName", arLst);  Here ‘cnnStr’ is connection string of BD, “UpdateName” is stored procedure name and arLst is ArrayList of arguments. By this array list arguments’ value passed to stored procedure. This has one more overload method, with extra argument DataSet, to use it’s DataTable to store retrieved value.<o:p>

<o:p>  How this code actually works<o:p>  

            SP class has one subclass SPArgBuild  which helps to pass argument to Stored Procedure. When spArgumentsCollection  method called then it internally creates an object of SPArgBuild, and set passed value to new objects’ attribute then return that object, which holds as an ArrayList element (here in arLst), and return ArrayList. In this way ArrayList holds Stored Procedure’s all argument. <o:p>

            Next when RunStoredProcedure called then data are retrieve from ArrayList and these set to command object’s parameter. Then send commend to execute Stored Procedure. <o:p>

<o:p>  An example by executing a sample Stored Procedure<o:p>

            To run that code we will follow these steps –<o:p>

(1)   Create a table

 CREATE TABLE client(
id int IDENTITY(1,1) NOT NULL,
Name varchar(64) NULL,
jdate datetime NULL
)  

(2)   Insert some rows in there<o:p>

INSERT INTO client(Name,jdate)VALUES ('Taniya',getdate())
INSERT INTO client(Name,jdate)VALUES ('Jhuma',getdate())
INSERT INTO client(Name,jdate)VALUES ('Ganesh',getdate()) 

(3)   Create a stored procedure<o:p>

create procedure UpdateName ( @nextName varchar(256), @id int )
as
update client set name=@nextName where id=@id 

(4)   Run stored procedure by my code. <o:p>

 ArrayList arLst = new ArrayList();//Create an Array List            
//Set argument data for Stored Procedure 
SP.spArgumentsCollection(arLst, "@nextName", "Suman Biswas", "varchar");
SP.spArgumentsCollection(arLst, "@id", "2", "int");
//Now run stored procedure.
SP.RunStoredProcedure(cnnStr, "UpdateName", arLst); 

 Now code is completed try it now.  


License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to add the out parameters Pin
rudragowda0721-Nov-14 0:40
rudragowda0721-Nov-14 0:40 
GeneralMy vote of 5 Pin
D-Kishore2-Sep-12 20:23
D-Kishore2-Sep-12 20:23 
my vote 4
General[My vote of 1] EntLib Pin
Mario Majčica23-Apr-09 3:29
professionalMario Majčica23-Apr-09 3:29 
GeneralMy vote of 1 Pin
TheGreatAndPowerfulOz2-Mar-09 12:01
TheGreatAndPowerfulOz2-Mar-09 12:01 
GeneralMy vote of 1 Pin
Dmitri Nеstеruk2-Mar-09 5:02
Dmitri Nеstеruk2-Mar-09 5:02 
GeneralGenerics Pin
BryH1-Mar-09 13:44
BryH1-Mar-09 13:44 
GeneralMy vote of 2 Pin
Member 162330418-Jan-09 17:19
Member 162330418-Jan-09 17:19 
General[Message Removed] Pin
Mojtaba Vali21-May-08 20:26
Mojtaba Vali21-May-08 20:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.