将 Stream 输入和输出 VarBinary(MAX) 列? (SQL 服务器)
我有一些正在序列化的数据。我需要在 .Net 3.5(还不是 4.0)下使用 ADO.NET(也使用企业库)从 VarBinary 列保存和恢复它。
我似乎能够找到并开始工作的唯一接口是使用 byte[]。这需要分配一个大数组来进行传输。
ADO.NET 似乎不可能不支持这些列的流。或者我忽略了什么?
谢谢
I have some data that I am serializing. I need to save and restore this from a VarBinary column using ADO.NET (also using Enterprise Library) under .Net 3.5 (not 4.0 yet).
The only interface I seem to be able to find and get working is by using a byte[]. This requires allocating a large array to do the transfer.
It seems impossible that ADO.NET doesn't support Streams for these columns. Or am I overlooking something?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我一直在寻找同样的答案。我在 EggHeadCafe 找到了一篇文章,它提供了一个使用如下命令进行读取的解决方案:
SELECT substring(DataColumn, @offset, @length) FROM BlobTable WHERE ID = @key
并且像这样的命令用于写入:
UPDATE BlobTable SET DataColumn.write(@buffer, @offset, @length) WHERE ID = @key
您可以在此处找到完整的解决方案(带有自定义流类): http://www.eggheadcafe.com/software/ aspnet/29988841/how-to-readwrite-chunked-to-varbinarymax-in-c.aspx。
不过,出于我的目的,我认为我将使用从
Sql Server 2008
开始提供的 FileStream 数据类型。一旦插入一条记录(无二进制数据),Sql Server 就会在磁盘上分配一个物理文件,用于读取和写入记录数据。然后运行命令来获取路径名:SELECT DataColumn.PathName() FROM BloblTable WHERE [Id] = @key
一旦你有了它,你就可以使用传统的流类来读取和写入文件,比如 System.IO.FileStream 。
开销相当大,但随着文件变大(大约 10mb),FileStream 会变得更快。对于较小的文件,您可以将 FileStream 列视为 varbinary 列,开销要少得多。 CodeProject 上的这篇文章解释了详细信息: http://www.codeproject.com/KB/database /SqlFileStream.aspx
I've been looking for the same answer. I found an article at EggHeadCafe that provides a solution using a command like this for reads:
SELECT substring(DataColumn, @offset, @length) FROM BlobTable WHERE ID = @key
And a command like this for writes:
UPDATE BlobTable SET DataColumn.write(@buffer, @offset, @length) WHERE ID = @key
You can find the complete solution (with a custom stream class) here: http://www.eggheadcafe.com/software/aspnet/29988841/how-to-readwrite-chunked-to-varbinarymax-in-c.aspx.
For my purposes though, I think I'm going to make use of the FileStream data type that's available as of
Sql Server 2008
. Once you insert a record (sans binary data), Sql Server allocates a physical file on the disk for reading and writing the record data to. You then run a command to get the path name:SELECT DataColumn.PathName() FROM BloblTable WHERE [Id] = @key
Once you have that, you can read and write to the file using a traditional stream class, like
System.IO.FileStream
.There is considerable overhead, but FileStream becomes faster as the files get larger (about 10mb). For smaller files, you can treat the FileStream column as a varbinary column with much less overhead. This article at CodeProject explains the details: http://www.codeproject.com/KB/database/SqlFileStream.aspx