2

I have the following method and stored procedure.

My question is how to return a string value from stored procedure to use in addStaffName method ?

public string addStaffName()
{
        string staffName = string.Empty;
        string sConnectionString = ConfigurationManager.ConnectionStrings["LGDB"].ToString();
        SqlConnection SqlCOn = new SqlConnection(sConnectionString);
        SqlCommand SqlCmd = new SqlCommand();

        SqlCOn.Open();
        SqlCmd.Connection = SqlCOn;
        SqlCmd.CommandType = CommandType.StoredProcedure;
        SqlCmd.CommandText = "FetchStaffName";
        SqlCmd.Parameters.AddWithValue("email", email);
        //???
        return staffName;
    }

create procedure fetchStaffName
    @email varchar(100)
AS 
begin 
    select (employeeName) 
    from employee 
    where email = @email
end

2 Answers 2

3

If you can be sure that you'llonly ever get one row, one column as a result set - then you can use .ExecuteScalar() on your SqlCommand like this:

string staffName = string.Empty;

string sConnectionString = ConfigurationManager.ConnectionStrings["LGDB"].ToString();

using (SqlConnection sqlCon = new SqlConnection(sConnectionString))
using (SqlCommand sqlCmd = new SqlCommand("FetchStaffName", sqlCon)
{
    sqlCmd.CommandType = CommandType.StoredProcedure;
    sqlCmd.Parameters.AddWithValue("@email", email);

    sqlCon.Open();

    staffName = sqlCmd.ExecuteScalar().ToString();

    sqlCon.Close();

    return staffName;
}

I also put the usage of SqlConnection and SqlCommand into using() { ... } blocks which is the recommended best practice for anything that's deriving from IDisposable to ensure proper disposal of the objects after their use

Sign up to request clarification or add additional context in comments.

3 Comments

thank u so much , nice advice , i can not vote u up , cause i new here !
but what if i wanted to get two columns as result from stored procedure ?
@AlirezaX: then you need to use .ExecuteReader() and you get back a SqlDataReader. With that you can read any number of columns, and any number of rows, and stick the results into objects of your choice
1

Consider to make fetchStaffName a scalar function rather than stored procedure. By definition stored procedures are required to perform a set of actions with data. Scalar function guarantees there will be exactly one output value for the set of input values.

CREATE FUNCTION fetchStaffName (
 @email VARCHAR(100)
)
RETURNS VARCHAR(?) --Place the length of employeeName field instead of ? symbol.
BEGIN
    RETURN (SELECT employeeName FROM employee WHERE email = @email)
END

And then your .NET code transforms into the following:

using (SqlConnection SqlCOn = new SqlConnection(sConnectionString))
{
    SqlCommand SqlCmd = new SqlCommand("SELECT fetchStaffName(@email)", SqlCOn);
    SqlCmd.CommandType = CommandType.Text;
    SqlCmd.Parameters.AddWithValue("@email", email);

    SqlCOn.Open();
    staffName = (string)SqlCmd.ExecuteScalar();
}

As you can see CommandType changed from StoredProcedure to Text. I also wrapped the work with SqlConnection object into using construction to dispose its resources and close automatically.

1 Comment

There are several ways of doing so. You can use stored procedure with multiple output parameters (a little bit different .NET and T-SQL syntax). Also you can select a row with several columns inside stored procedure and then use ExecuteReader() method of your SqlCommand. For details create a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.