微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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进出数据库.

原文地址:https://www.jb51.cc/csharp/97245.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐