evgomes / supermarket-api

Simple RESTful API built with ASP.NET Core and .NET 8 to show how to create RESTful services using a decoupled, maintainable architecture.

Home Page:https://medium.com/free-code-camp/an-awesome-guide-on-how-to-build-restful-apis-with-asp-net-core-87b818123e28

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How about retrievingdata from a stored procedure?

Pham-Tuan-Phat opened this issue · comments

This sample is very useful.
However, it is better if you can show how to receive data from a stored procedure. (one recordset or more returned from a stored procedure).

Thank a lot.

Hi,

For this API example, I haven't used a real database, such as SQL Server, to store data, since it's not a real application, so I can't really implement an example here, but I'll show you a way to do this.

If you're not using EF Core as ORM to handle data access, you need to use ADO.NET.

Imagine we have a SQL Server database for this API. To access it without using EF Core, we need to install the package System.Data.SqlClient to access our database.

Once you install this package (you can execute the .NET CLI command on the command line that is specified here), you can implement repositories that use ADO.NET to query data.

Here is an example of the CategoryRepository using ADO.NET to list categories:

public class CategoryRepositoryAdo : ICategoryRepository {
	private const string SQL_LIST_CATEGORIES = @"
		SELECT
			Id,
			Name
		FROM
			Categories
	";

	public async Task<IEnumerable<Category>> ListAsync () {
		var categoriesList = new List<Category> ();

		using (var connection = new SqlConnection ("here_goes_the_connection_string")) // First, we open a connection, specifying the connection string.
		using (var command = connection.CreateCommand ()) // Then we create a command, to specify what we want to query.
		{
			command.CommandType = CommandType.Text; // Here you have to specify CommandType.StoredProcedure
			command.CommandText = SQL_LIST_CATEGORIES; // Here you specify the procedure name

			connection.Open (); // Before executing the command, we have to open the connection.
			using (var reader = await command.ExecuteReaderAsync ()) // Then we execute the command returning a SqlDataReader, to loop through the resultset.
			{
				while (await reader.ReadAsync ()) // We then use a "while" loop to iterate over all returned items
				{
					var category = new Category 
					{
						Id = reader.GetInt32("Id"), // Here we get the values of each item, using the correct casting method
						Name = reader.GetString("Name"),
					};

					categoriesList.Add(category);
				}
			}

			connection.Close(); // Don't forget to close the connection. The "using" statements are also important to avoid keeping resources allocated after finishing the query.
		}

		return categoriesList;
	}

	// Other methods
}

For this example, I used an inline query to show what I'm executing (the constant SQL_LIST_CATEGORIES shows the query). You can replace it with a stored procedure name following the instructions I've written as comments.

Here are better samples if you want to learn more. I hope it helps you.

Thank you.