3

我有一些要序列化的数据。我需要在 .Net 3.5(还不是 4.0)下使用 ADO.NET(也使用企业库)从 VarBinary 列中保存和恢复它。

我似乎能够找到并开始工作的唯一接口是使用 byte[]。这需要分配一个大数组来进行传输。

ADO.NET 似乎不可能不支持这些列的流。还是我忽略了什么?

谢谢

4

1 回答 1

5

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

于 2010-11-26T13:27:05.483 回答