简体   繁体   English

如何有效地将大数据从SQL Server 2008流传输到Web浏览器?

[英]How can I efficiently stream large data from SQL Server 2008 to a web browser?

I'm using C# and ASP.NET MVC 3. Currently, I'm taking a large set of data from SQL Server, adding it into a StringBuilder , and then using System.Text.ASCIIEncoding to convert the StringBuilder object to a byte[] array. 我正在使用C#和ASP.NET MVC3。目前,我正在从SQL Server中获取大量数据,将其添加到StringBuilder ,然后使用System.Text.ASCIIEncodingStringBuilder对象转换为byte[]数组。

I then send the byte[] array to the view. 然后,我将byte[]数组发送到视图。 However, the amount of data is so large, I get the following exception: 但是,数据量如此之大,我得到以下异常:

Exception of type 'System.OutOfMemoryException' was thrown 引发了类型'System.OutOfMemoryException'的异常

Here's the code that I use in my controller to convert the StringBuilder object to byte[] and then return to the view. 这是我在控制器中使用的代码,用于将StringBuilder对象转换为byte[] ,然后返回视图。 Is there a way to stream the data straight from the database to the client browser without loading it all up in memory on the server first? 有没有一种方法可以将数据直接从数据库流传输到客户端浏览器,而无需先将所有数据首先加载到服务器的内存中?

System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
return File(encoding.GetBytes(csv.ToString()), "text/csv", "Query_Export.csv");

You can do something similar to the following pseudo code 您可以执行类似于以下伪代码的操作

  while (sqlReader.GetBytes(params))//params is a placeholder for the actual arguments and
                                    //will have a byte array buffer and some counter indexes
  {
      context.Response.BinaryWrite(buffer);
      //somecounter
  }

SqlDataReader http://msdn.microsoft.com/en-in/library/system.data.sqlclient.sqldatareader.aspx SqlDataReader http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM