我从数据库中提取 BLOB 并将其存储在字节数组中。最初它被定义为与数据库上的 BLOB 列大小相同,相当大。
int maxsize = 20971520;
int thisSize;
Byte[] picture = new Byte[maxsize];
所以我捕获了这个 Blob :
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 列的实际大小?
最佳答案
为什么不查询字段的长度,以便第一次时 byte[] 的大小是正确的。
来自MSDN
If you pass a buffer that is null, GetBytes returns the length of the entire field in bytes, not the remaining size based on the buffer offset parameter.
所以你的代码将变成
long length = rdr.GetBytes(3, 0, null, 0, maxsize);
Byte[] picture = new Byte[length];
rdr.GetBytes(3, 0, picture, 0, length);
关于c# - 确定 BLOB 列的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4647942/