I currently have a MVC C# web application and I am trying to create an action result that acts kind of like an API. This API method runs a stored procedure and returns the results in JSON format as the response.
Problem is, in some instances the stored procedure is returning ~6.5k rows, and all that data is coming into memory of my application and blowing it up. (there's a good bit of columns to this data, all required)
So my question is: is there a way to run a stored procedure and return its results to the client without bringing the whole result set into memory on the server? Maybe I can take the response sql stream and feed it to the response stream?
My current code contains the memory leak and is as follows:
public ActionResult GetOutletsByHierarchyLevel(string SearchTerm, string Level, bool ReturnAll = false)
{
...
var data = db.CollectionFromSql(
"EXEC [apps].[Rapport_GetOutletsByHierarchyLevel] @SearchTerm,@HierarchyLevel,@ReturnAll",
new Dictionary<string, object>
{{"@SearchTerm", SearchTerm}, {"@Level", Level}, {"@ReturnAll", ReturnAll}}
);
var jsonResult = Json(new JSONResponse()
{
Success = true,
Data = data.ToList().DynamicSQLToDictionary()
});
data = null;
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
Let me know if there's anything else I can supply you all with. Thanks in advance for any recommendations/articles!