Click here to Skip to main content
15,905,776 members
Articles / Web Development / ASP.NET
Article

Calling SQL Server stored procedures from Microsoft.NET

Rate me:
Please Sign up or sign in to vote.
1.00/5 (15 votes)
29 Feb 2008CPOL2 min read 34.1K   14   6
This article is intended to illustrate how to call SQL Server stored procedure from Microsoft.NET

Introduction

Here I assume that reader knows how to write SQL Server stored procedure. My intension is to describe different ways of calling a stored procedure. I try to keep it as easy as simple.

Background

I wanted to create an ASP.NET page that would pass parameters to a SQL Server stored procedure to process data into a database. It surprised me that I couldn't find online sample through Google, which motivated me to put together this short article.

Using the code

At first I want to show how we can call a simple stored procedure which has no parameters. To do this let Database has a simple stored procedure named SP1. So we can call the procedure in the following way:

string sConstr = ConfigurationManager.ConnectionStrings["urconnectionstring "].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "SP1";

SqlConnection Conn = new SqlConnection(sConstr);
Conn.Open();

sqlCmd.Connection = Conn;
sqlCmd.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(sqlCmd);

DataTable dt = new DataTable("tbl");
da.Fill(dt);

sqlCmd.Dispose();
Conn.Close();

Now I want to give an example on how we can call a stored procedure which has input parameter. Let first parameter is @Param1 and the second one is @Param2. So looks at the below code segment:

string sConstr = ConfigurationManager.ConnectionStrings["urconnectionstring "].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "SP2";

SqlConnection Conn = new SqlConnection(sConstr);
Conn.Open();

sqlCmd.Connection = Conn;
sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd.Parameters.Add(new SqlParameter("@Param1", SqlDbType.Real, 18));
sqlCmd.Parameters["@Param1"].Value = 8;

sqlCmd.Parameters.Add(new SqlParameter("@Param2", SqlDbType.Real, 18));
sqlCmd.Parameters["@Param2"].Value = 10;

SqlDataAdapter da = new SqlDataAdapter(sqlCmd);

DataTable dt = new DataTable("tbl");
da.Fill(dt);

sqlCmd.Dispose();
Conn.Close();

In the above way we can call stored procedure which has parameters. If anyone examines the above code module they will see that here I bind parameters one after another which is not an efficient way especially when anyone thinks about reusability. Now my concern is that how we can write a method which executes the stored procedures & we can reuse it. My approach is in each application we use some common utility classes. May be UI utility/DB utility. So one can write a static DB utility class where he can write the below static module named ExecSP:

  public static DataTable ExecSP(string procName, string[] paramName, Object[] paramValue)
  {
      string sConstr = ConfigurationManager.ConnectionStrings["urconnectionstring"].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
      sqlCmd.CommandText = procName;

      for (int i = 0; i < paramName.Length; i++)
          sqlCmd.Parameters.AddWithValue(paramName[i], paramValue[i]);

      SqlConnection Conn = new SqlConnection(sConstr);
      Conn.Open();

      sqlCmd.Connection = Conn;
      sqlCmd.CommandType = CommandType.StoredProcedure;

      SqlDataAdapter da = new SqlDataAdapter(sqlCmd);

      DataTable dt = new DataTable("tbl");
      da.Fill(dt);

      sqlCmd.Dispose();
      Conn.Close();
      return dt;
  }

This method takes 3 parameters. The first one is the stored procedure name, second one is the parameters name & the last one is corresponding parameters value. Now I want to show how we invoke the procedures from UI using above method:

DataTable dt = new DataTable();
dt=clsDbUtility.ExecSP("SP2", new string[] { "@Param1","@Param2" }, new object[] { 8,10 });

Also anyone can use the above static method to invoke procedures which has no parameter in the following way:

DataTable dt = new DataTable();
dt = clsDbUtility.ExecSP("Sp1", new string[] { }, new object[] { });

License

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


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

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 862979010-May-12 19:59
Member 862979010-May-12 19:59 
General[My vote of 1] old skool Pin
joejoeyeahbaby21-Mar-09 13:09
joejoeyeahbaby21-Mar-09 13:09 
dude, you really gotta read newer material. this approach is valid but circa 5 years ago.
GeneralThis is really poor Pin
Bert delaVega29-Feb-08 11:52
Bert delaVega29-Feb-08 11:52 
GeneralLearn then write.... Pin
Gevorg29-Feb-08 8:26
Gevorg29-Feb-08 8:26 
GeneralPretty Good Pin
CoolDadTx29-Feb-08 2:44
CoolDadTx29-Feb-08 2:44 
GeneralRe: Pretty Good Pin
shawpnendu29-Feb-08 5:07
shawpnendu29-Feb-08 5:07 

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.