Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确定BLOB列的大小_C#_Arrays_Resize - Fatal编程技术网

C# 确定BLOB列的大小

C# 确定BLOB列的大小,c#,arrays,resize,C#,Arrays,Resize,我从数据库中提取一个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.

我从数据库中提取一个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小得多,因此如何将字节数组调整为blob列的实际大小?

使用为什么不查询字段的长度,以便字节[]第一次是正确的大小

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

因此,您的代码将成为

long length = rdr.GetBytes(3, 0, null, 0, maxsize);

Byte[] picture = new Byte[length];

rdr.GetBytes(3, 0, picture, 0, length);