0

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

11
  • Your Web API won't be using SSMS - thats purely a client UI. Commented Mar 29, 2022 at 7:12
  • What is your _context? Entity Framework? Commented Mar 29, 2022 at 7:14
  • There seem to be a lot of examples of how to do this out there. What resources have you checked out? Where are you stuck? Commented Mar 29, 2022 at 7:15
  • @DaleK Yes _context I tried using Entity Framework. But I know it is not correct implementation. Commented Mar 29, 2022 at 7:21
  • 1
    Calling an sp is pretty much the same regardless of whether it returns data. You just don't get anything back Commented Mar 29, 2022 at 7:24

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

my intelligence is not showing ExecuteSqlCommand

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.