I am new to ASP.NET Core (3.1). I have a stored procedure that takes two parameters: startDate and endDate.
It returns the following result sets in one go (or execution):

I want to be able to grab the result sets in 3 separate variables because I will like to output them on my view:
For example - for the 1st result set:
foreach (var maindata in Model.MainDataViewModel)
{
maindata.CaseId
maindata.EpisodeId
maindata.SpecimenTag
maindata.PathologyOrderId
}Likewise for the second and third.
I have attempted to call the stored procedure using this code:
public SpPotentialCandidatesForOncotypeDXViewModel GetPotentialCandidatesForTheOncotypeDXData(DateTime startDate, DateTime endDate)
{
try
{
List pc = new List
{
new SqlParameter("@p0", startDate),
new SqlParameter("@p1", endDate),
};
var da = _dbContext.Database.ExecuteSqlRaw("spPotentialCandidatesForOncotypeDX @p0, @p1", pc.ToArray());
}
catch (Exception ex)
{
AppLog.WriteError("GetPotentialCandidatesForTheOncotypeDXData", ex.Message);
}
// return spPotentialCandidatesForOncotypeDXes;
}After testing, da has value -1. Via further research I realise ExecuteSqlRaw returns the number of rows affected and not the result sets.
How can I capture these result sets individually and assign to variables?

Prasad RaveendranRank #320 · 7.8K reputationPosted Aug 18, 2023, 11:56 PM EDT
Using ADO.NET, you can achieve the same as the below. You may need to update this script depends on your use case
using System; using System.Data; using System.Data.SqlClient; namespace StoredProcedureExample { class Program { static void Main(string[] args) { string connectionString = "your_connection_string_here"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("YourStoredProcedureName", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@startdate", yourStartDateValue); command.Parameters.AddWithValue("@enddate", yourEndDateValue); using (SqlDataReader reader = command.ExecuteReader()) { // Read the first result set while (reader.Read()) { int caseID = (int)reader["CaseID"]; string caseName = reader["CaseName"].ToString(); // Process caseID and caseName } // Move to the next result set reader.NextResult(); // Read the second result set while (reader.Read()) { string firstName = reader["FirstName"].ToString(); string lastName = reader["LastName"].ToString(); string designation = reader["Designation"].ToString(); // Process firstName, lastName, and designation } // Move to the next result set reader.NextResult(); // Read the third result set while (reader.Read()) { string returnValue = reader["ReturnValue"].ToString(); // Process returnValue } } } } } } }Prasad RaveendranRank #320 · 7.8K reputationPosted Aug 19, 2023, 12:11 AM EDT
if you have patience, requesting you to go through the below links . This may give you some insights on multi result sets.
I would recommend to split this into three stored procedure and invoke it.
https://github.com/nilendrat/EfCoreMultipleResults
https://github.com/dotnet/efcore/issues/6026
https://github.com/KishorNaik/Sol_EF_SqlQuery
https://github.com/dotnet/efcore/issues/8127
Divine Fokwa22 reputationPosted Aug 21, 2023, 6:52 PM EDT
Many thanks @Prasad Raveendran
You saved me many hours. Thank you