c# - 确定 BLOB 列的大小

标签 c# arrays resize

我从数据库中提取 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/

相关文章:

c# - 在 XAML 中设置描述属性 - ParseException

c# - 如何在for循环中每次更新标签

c++ - Visual Studio - 缓冲区溢出

javascript - 调整大小时不会重新计算附加位置

html - 调整大小时干净地隐藏页面元素

c# - TreeView 替代方案

c# - 搜索文件到 Ntfs

java - 如何对数组进行排序并跟踪 java 中的索引

php - 在 PHP 中迭代多维数组时出现问题

html - 如何调整到合适的img标签?