I was working on an existing project in which I need to call stored procedures in asp net Core without Entity Framework. In the Database, I have created some stored procedures and we want to call the store procedure in our code but we do not want to use EF.
To call a stored procedure in a Web API without using Entity Framework, you can use ADO.NET’s SqlConnection and SqlCommand classes.
Here’s an example code snippet:
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("YourStoredProcedureName", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@parameter1", value1); command.Parameters.AddWithValue("@parameter2", value2); connection.Open(); SqlDataReader reader = command.ExecuteReader(); // Process the returned data from the stored procedure } }
In this example, you need to replace connectionString with the actual connection string to your database, and YourStoredProcedureName with the name of your stored procedure.
You can also pass in any required parameters to the stored procedure using the command.Parameters.AddWithValue() method. Once you execute the command, you can process the returned data using a SqlDataReader or any other suitable data reader class.