Click here to Skip to main content
15,891,951 members
Articles / All Topics

Executing a Stored Procedure from a WCF Service Method

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
26 Nov 2011CPOL 24.1K   3  
Executing a Stored Procedure from a WCF Service Method

Here is an example of a method that is used within a WCF Service to execute a Stored Procedure and parse the return value (a success/failure string) to a local variable which can be manipulated to display a friendly success or failure message.

C#
public String fSave (string aParm1, string aParm2, 
                             string aParm3, Int32 aParm4)
{
    SqlConnection lSQLConn = null;
    SqlCommand lSQLCmd = new SqlCommand();
    string lsResponse = "";
    string connStr = "";
 
    connStr = 
    ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString;
 
    try
    {
        // create and open a connection object
        lSQLConn = new SqlConnection(connStr);
        lSQLConn.Open();
        //The CommandType must be StoredProcedure if we are using an ExecuteScalar
        lSQLCmd.CommandType = CommandType.StoredProcedure;
        lSQLCmd.CommandText = "sp_YourSPName"; 
        lSQLCmd.Parameters.Add(new SqlParameter("@Parm1", aParm1));
        lSQLCmd.Parameters.Add(new SqlParameter("@Parm2", aParm2));
        lSQLCmd.Parameters.Add(new SqlParameter("@Parm3", aParm3));
        lSQLCmd.Parameters.Add(new SqlParameter("@Parm4", aParm4));
               
        lSQLCmd.Connection = lSQLConn;
        //Executes the SP and returns the single select output to a variable
        lsResponse = Convert.ToString(lSQLCmd.ExecuteScalar());
    }
    catch (Exception Exc)
    {
        return "Error: " + Exc.Message;                
    }
    finally
    {
        lSQLCmd.Dispose();
        lSQLConn.Close();
    }
 
    if (String.IsNullOrEmpty(lsResponse))
    {
        return "Error: Unspecified problem while adding task.";                
    }
    return lsResponse;
}

Now, let's review the above method. There are a few interesting things:
Once the connection is open, the SQLCommand has to be defined as a CommandType = CommandType.StoredProcedure. This signals the type of action we need to execute.
Next, we define the Stored Procedure to be executed by defining the CommandText="sp_YourSPName".
To get the return value, we set the string variable to the results of a SQL Command, ExecuteScalar().

But wait. What should we do if our Stored Procedure returns a result set rather than a single value?
Well, you need to make a slight change to the method, we need to add a DataAdapter and a DataSet to parse the result.
See below:

C#
SqlConnection lSQLConn = null;
SqlCommand lSQLCmd = new SqlCommand();
//Declare a DataAdapter and a DataSet
SqlDataAdapter lDA = new SqlDataAdapter();
DataSet lDS = new DataSet();
 
//...Execution section
 
// create and open a connection object
lSQLConn = new SqlConnection(connStr);
lSQLConn.Open();
//The CommandType must be StoredProcedure if we are using an ExecuteScalar
lSQLCmd.CommandType = CommandType.StoredProcedure;
lSQLCmd.CommandText = "sp_YourSPName"; 
lSQLCmd.Parameters.Add(new SqlParameter("@Parm1", aParm1));
lSQLCmd.Parameters.Add(new SqlParameter("@Parm2", aParm2));
lSQLCmd.Parameters.Add(new SqlParameter("@Parm3", aParm3));
lSQLCmd.Parameters.Add(new SqlParameter("@Parm4", aParm4));
 
lSQLCmd.Connection = lSQLConn;
//Fill the DataAdapter with a SelectCommand
lDA.SelectCommand = lSQLCmd;
lDA.Fill(lDS);

Hope this is helpful,
Will


This article was originally posted at http://feeds.feedburner.com/blogspot/NyVKAz

License

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


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

Comments and Discussions

 
-- There are no messages in this forum --