【问题标题】:Determine size of a BLOB column确定 BLOB 列的大小
【发布时间】:2011-06-06 14:01:52
【问题描述】:

我从数据库中提取一个 BLOB 并存储在一个字节数组中。最初它被定义为与 DB 上的 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 列的实际大小?

【问题讨论】:

    标签: c# arrays resize


    【解决方案1】:
    【解决方案2】:

    为什么不查询字段的长度,所以第一次 byte[] 是正确的大小。

    来自MSDN

    如果你传递一个为空的缓冲区, GetBytes 返回的长度 整个字段以字节为单位,而不是 基于缓冲区的剩余大小 偏移参数。

    所以你的代码会变成

    long length = rdr.GetBytes(3, 0, null, 0, maxsize);
    
    Byte[] picture = new Byte[length];
    
    rdr.GetBytes(3, 0, picture, 0, length);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2020-04-14
      • 2010-11-10
      • 1970-01-01
      • 2017-10-31
      • 2012-05-25
      相关资源
      最近更新 更多