I am trying to fix an issue with an existing code and not sure if this approach is correct or not as I have not worked with dapper before. I am trying to return a list of objects using a stored procedure.
The code below compiles but I am not sure if this is the correct way to return multiple rows or not. Is there anything that just returns to list rather than me looping through and constructing list and then returning?
public async Task<List<Event>> GetEventsAsync()
{
await using SqlConnection db = new(this.settings.ConnectionString);
List<Event> events = new List<Event>();
var eventsMarkedPublished = await db.QueryAsync<Event>("[sp_get_published_events]", null, commandTimeout: 60, commandType: CommandType.StoredProcedure);
foreach (var pubEvents in eventsMarkedPublished)
{
events.Add(pubEvents);
}
return events;
}
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!