OK. Reading doc for IDataRecord.GetBytes method I've found the solution.
We can retrieve blob data size directly from datareader passing null instead of buffer object.
long file_size;
file_size = dr.GetBytes(dr.GetOrdinal("file_content"), 0, null, 0, 0); // here we are retrieving size
byte[] buffer = new Byte[file_size]; // allocating buffer
if (File.Exists(path)) File.Delete(path); // remove old file if exists
out_file = File.OpenWrite(path); // open new stream for write
file_size = dr.GetBytes(dr.GetOrdinal("file_content"), 0, buffer, 0, (int)file_size); // get whole blob into buffer
out_file.Write(buffer, 0, (int)file_size); // write buffer into file
please note, that GetBytes returns long but size of grabbed data is given by int. So, notation in my code is correct only for blobs up to 2GB. For larger blobs you have to do loop and dump data partialy.
best regards
Edited 1 time(s). Last edit at 03/10/2007 11:10AM by Michal Kozusznik.