我从一个数据库中拉取一个BLOB并存储在一个字节数组中。最初,它被定义为与DB上的BLOB列大小相同,这是相当大的。
int maxsize = 20971520;
int thisSize;
Byte[] picture = new Byte[maxsize];所以我抓取了这个斑点:
rdr.GetBytes(3, 0, picture, 0, maxsize);然后将其写入磁盘:
FileStream fstream = new FileStream(ImageFullName,FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriter bwriter = new BinaryWriter(fstream);
bwriter.Write(picture);
bwriter.Flush();
bwriter.Close();
fstream.Close();问题是这些blob中大多数都比maxsize小得多,那么如何将Byte数组调整为blob列的实际大小呢?
发布于 2011-01-10 22:46:02
为什么不查询一下字段的长度,这样第一次byte[]的大小就是正确的。
来自MSDN
如果传递的缓冲区为null,则GetBytes返回整个字段的长度(以字节为单位),而不是根据缓冲区偏移量参数返回剩余大小。
所以你的代码会变成
long length = rdr.GetBytes(3, 0, null, 0, maxsize);
Byte[] picture = new Byte[length];
rdr.GetBytes(3, 0, picture, 0, length);发布于 2011-01-10 22:44:14
使用Array.Resize(ref T[] array, int newSize)
https://stackoverflow.com/questions/4647942
复制相似问题