Home > OS >  How run stored procedure in ASP.NET Core 6 Web API using C#?
How run stored procedure in ASP.NET Core 6 Web API using C#?

Time:08-06

I want run a stored procedure from a controller in ASP.NET Core 6 Web API, but always get an error 500.

I want to send 2 parameters and receive a list.

This is the stored procedure:

ALTER PROCEDURE [dbo].[listaProductoPrecio]
    @Minimo int,
    @Maximo int
AS
BEGIN
    SELECT * 
    FROM dbo.Productos 
    WHERE PrecioProducto BETWEEN @Minimo AND @Maximo
END

And this is the controller:

[HttpGet]
[Route("api/listaEntrePrecios")]
public IEnumerable<Producto> listaEntrePrecios(int minimo, int maximo)
{
    var parameters = new[]
            {
                new SqlParameter("@Minimo", minimo),
                new SqlParameter("@Maximo", maximo)
            };

    var lista = context.productos.FromSqlRaw("EXECUTE listaProductoPrecio @Minimo @Maximo", parameters).ToList();

    return lista;
}

I do that to try and run a stored procedure in an API, help please

CodePudding user response:

You are simply missing a comma:

"EXECUTE listaProductoPrecio @Minimo @Maximo"

should be

"EXECUTE listaProductoPrecio @Minimo, @Maximo"
  • Related