Large query how to return results back in sections C#/ASP.NET MVC 4

喜你入骨 提交于 2019-12-11 04:38:28

问题


I have an application written in ASP.NET MVC 4. I have a requirement to return large results from a table accessed with oledbdatareader.

I am using AJAX to return a JsonResult object that contains a List: List<TableRow>

What I do not understand is, if I am in the DataReader loop

using (OleDbDataReader reader = command.ExecuteReader())
{
   while (reader.Read())
   {
       names.Add(Convert.ToString(reader[0]));
   }
}

Is there a way to periodically send the list object, and then create a new object to pickup and continue on?


回答1:


Technically the server can only return a single response to every request, so there is not way to do what you want (short of setting up some sort of crazy socket stuff).

I'd flip what you're doing on its head and have your javascript request chunks of the dataset in batches of 1000 (or whatever size), and have it start rendering while requesting the next chunk.

Better yet, you could implement some form of infinite scrolling in your UI so that the next chunk is only requested just in time for it to be displayed, that way you're not sending unneeded data to the client.




回答2:


I think you have a few options that are rather common to implement. If you have 10,000 records that you need to give back to the client, you can manage this in your MVC application. If you are using Entity Framework and Linq, you can write your business logic to just send back 100 rows per each time the user clicks the next button. This will keep the transmission to the client small and even keep the call from the web server to the SQL server small.

If you don't want to have the user click a next button (i.e. paging) but want do to an infinite scroll style, then just do the same thing, as the user keeps scrolling, just keep calling the Ajax method to send back each 100 rows at a time.

The web server and database server isn't going to choke on 10,000 records; it will be choking going down to the client. Even if you open a socket in Signal R, I think you should ask yourself do I really need to push 10,000 rows all at once to the client?

Think about the Twitter app on a mobile phone, they are sending the data to you as you scroll, they are not sending it all at once. Does that help any?

Updated based on your comment regarding its straight SQL

Here is an example of doing a simple version of paging in SQL:

DECLARE @intStartRow int;
DECLARE @intEndRow int;

SET @intStartRow = (@intPage -1) * @intPageSize + 1;
SET @intEndRow = @intPage * @intPageSize;    

WITH blogs AS
    (SELECT strBlogName, 
     ROW_NUMBER() OVER(ORDER BY intID DESC) as intRow, 
     COUNT(intID) OVER() AS intTotalHits 
     FROM tblBlog)
SELECT strBlogName, intTotalHits FROM blogs
WHERE intRow BETWEEN @intStartRow AND @intEndRow

Source: http://joelabrahamsson.com/my-favorite-way-to-do-paging-with-t-sql/



来源:https://stackoverflow.com/questions/42935876/large-query-how-to-return-results-back-in-sections-c-asp-net-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!