blac040
blac040

Reputation: 127

Call stored procedure to input data in database from ASP.NET Web API?

I want to insert the data from ASP.NET Web API to my data in SQL Server in pre defined table. First I am getting response after calling REST API and want to insert the response in table in database.

API Controller

[HttpGet]
public async Task<IActionResult> GetOutput(int id)
{
    string CheckJob = "exec sp_GetJob "+ "@id= " + id;

    var result = _context.Job.FromSqlRaw(CheckJob).ToList().FirstOrDefault();
    if(result == null){
        string InsertResult = "exec sp_add" + "@result = " + result;
        _context.FromSqlRaw(InsertResult);  <-- Don't know how to call stored procedure
    }

    return Ok(result);
}

Stored Procedure

create procedure sp_add @result int
as
begin 
    insert into School(id, created_date)
        values (@result, GetDate());
end

I am facing issue in calling stored procedure to insert data. Can somebody please help me out?

Thank you

Upvotes: 0

Views: 1056

Answers (1)

Serge
Serge

Reputation: 43860

since you dont expect any data to return, try this

_context.Database.ExecuteSqlCommand(result);

or async with params

var resultParam = new SqlParameter("@result", result);
await   context.Database.ExecuteSqlCommandAsync("EXEC  sp_add @result", resultParam);

Upvotes: 1

Related Questions