简体   繁体   English

在C#中从SQL Server流式传输VARBINARY数据

[英]Streaming VARBINARY data from SQL Server in C#

I'm trying to serve image data stored in a VARBINARY(MAX) field in the database using ASP.Net. 我正在尝试使用ASP.Net为存储在数据库中VARBINARY(MAX)字段中的图像数据提供服务。 Right now, the code is filling a data table, then pulling the byte array out of the DataRow and pushing the byte array into the response. 现在,代码正在填充数据表,然后将字节数组从DataRow中拉出并将字节数组推入响应中。 I'm wondering if there's a way to more-or-less stream the data from the SQL Server into the response without having to marshal around these huge byte arrays (since the images are large, they cause OutOfMemoryExceptions). 我想知道是否存在一种方法,可以将SQL Server中的数据或多或少地流式传输到响应中,而不必在这些巨大的字节数组之间进行编组(由于图像很大,它们会导致OutOfMemoryExceptions)。 Is there a class/mechanism for that? 是否有一个阶级/机制?

The current code looks more or less like: 当前代码或多或少类似于:

DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(commandText, connectionString);
adapter.Fill(table);
DataRow row = table.Rows[0];
byte[] imageData = row[0] as byte[];
if(imageData != null)
{
  Response.Clear();
  Response.BinaryWrite(imageData);
  Response.End();
}

Thanks in advance - any help is appreciated. 在此先感谢您-我们将不胜感激。

See Download and Upload Images from SQL Server for an article covering the topic, including efficient streaming semantics. 有关涵盖该主题的文章,请参阅从SQL Server下载和上传图像 ,包括有效的流语义。 You must use a SqlDataReader opened with CommandBehavior.SequentialAccess : 您必须使用通过CommandBehavior.SequentialAccess打开的SqlDataReader

SequentialAccess Provides a way for the DataReader to handle rows that contain columns with large binary values. SequentialAccess为DataReader提供一种方法来处理包含具有大二进制值的列的行。 Rather than loading the entire row, SequentialAccess enables the DataReader to load data as a stream. SequentialAccess无需加载整个行,而是使DataReader可以将数据作为流加载。 You can then use the GetBytes or GetChars method to specify a byte location to start the read operation, and a limited buffer size for the data being returned. 然后,您可以使用GetBytes或GetChars方法指定一个字节位置来启动读取操作,并为返回的数据指定一个有限的缓冲区大小。

The linked article provides full code for creating a Stream backed by an SqlDataReader, you can simply Stream.CopyTo (HttpResponse.OutputStream) , or use a byte[] chunked copy if you don't have .Net 4.0 yet. 链接的文章提供了用于创建由SqlDataReader支持的Stream的完整代码,您可以简单地Stream.CopyTo (HttpResponse.OutputStream) ,也可以使用byte []分块副本(如果您还没有.Net 4.0)。

This follow up article explains how to use a FILESTREAM column for efficient streaming of large VARBINARY data in and out of the database. 这篇后续文章介绍了如何使用FILESTREAM列来有效地将大型VARBINARY数据进出数据库。

@Remus's answer above is out-of-date since .NET 4.5 introduced first-class SqlClient Streaming . 自.NET 4.5引入一流的SqlClient Streaming以来,@ Remus的答案已经过时。 It's no longer necessary to access the SqlDataReader's GetBytes() methods to get a stream from a SQL Query. 不再需要访问SqlDataReader的GetBytes()方法来从SQL查询获取流。

Now you simply call SqlDataReader.GetStream(int) to get a stream over a blob column. 现在,您只需调用SqlDataReader.GetStream(int)即可通过Blob列获取流。

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

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