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

我正在尝试使用ASP.Net为数据库中存储在VARBINARY(MAX)字段中的图像数据提供服务.现在,代码正在填充数据表,然后将字节数组拉出DaTarow,并将字节数组推入响应.我想知道是否有一种方法来将数据从sql Server更多或更少地传输到响应中,而不必围绕这些巨大的字节数组进行编组(因为图像很大,它们导致OutOfMemoryExceptions).是否有类/机制?

当前代码看起来或多或少如下:

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();
}

感谢提前 – 任何帮助是赞赏.

解决方法

请参阅 Download and Upload Images from SQL Server有关该主题文章,包括高效的流语义.您必须使用 SqlDataReader打开的 SqlDataReader

SequentialAccess Provides a way for
the DataReader to handle rows that
contain columns with large binary
values. Rather than loading the entire
row,SequentialAccess enables the
DataReader to load data as a stream.
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.

链接文章提供了用于创建由sqlDataReader支持的流的完整代码,您可以简单地使用Stream.CopyTo(HttpResponse.OutputStream),或者如果您还没有.Net 4.0,则使用byte []块复制.

该后续文章解释了how to use a FILESTREAM column for efficient streaming of large VARBINARY data进出数据库.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...