简体   繁体   中英

Return a custom message from SQL Server stored procedure to Web API C#

I have this API, which calls a stored procedure to insert a value and it has few PRINT statements in the stored procedure

public class ConfirmJobController : ApiController
{
    [HttpPost]
    public IHttpActionResult Post(JobConfirmation confirmation)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            string outputMessage;

            con.Open();
            SqlCommand cmd = new SqlCommand("usp_PhoneApp_ConfirmBooking", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@p_jobId", confirmation.JobID);
            cmd.Parameters.AddWithValue("@p_jobStatus", confirmation.JobStatus);
            cmd.Parameters.AddWithValue("@p_createdDate", confirmation.CreatedDate);

            cmd.ExecuteNonQuery();

            con.InfoMessage += delegate (object sender1, SqlInfoMessageEventArgs e1)
            {
                outputMessage = e1.Message;
            };

            return outputMessage;

        }
    }
}

I would like get custom message from the stored procedure to the return value of the API. How can I do this?

currently the line return outputMessage gives an error with "use of unassigned variable and cannot implicitly convert string to IHttpActionResult"

Any help is greatly appreciated.

To answer only the question in the title, you could add an output parameter to the stored procedure, that's what I usually use.

https://technet.microsoft.com/en-us/library/ms187004.aspx

The other issues are simply a matter of returning the correct type that the method declares. Return a ContentResult or some other object that implements the declared interface.

You can make use of ExecuteNonQuery to return int value, method looks like inserting data via stored procedure. Alternately you can also make use out parameter in stored procedure for any specific success message.

[HttpPost]
public IHttpActionResult Post(JobConfirmation confirmation)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        string outputMessage;

        con.Open();
        SqlCommand cmd = new SqlCommand("usp_PhoneApp_ConfirmBooking", con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@p_jobId", confirmation.JobID);
        cmd.Parameters.AddWithValue("@p_jobStatus", confirmation.JobStatus);
        cmd.Parameters.AddWithValue("@p_createdDate", confirmation.CreatedDate);

        if(cmd.ExecuteNonQuery() > 0)
           outputMessage = "Success";
        else
           outputMessage = "Failed";

        return Ok(outputMessage);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM