Exemplo n.º 1
0
        public string getBLOBFile(string fileName, MySQLDataReader reader, int fieldnumber)
        {
            FileStream   fs;
            BinaryWriter bw;
            int          bufferSize = 4096;

            byte[] outbyte = new byte[bufferSize];
            long   retval;
            long   startIndex = 0;

            try
            {
                // Reset the starting byte for the new BLOB.
                startIndex = 0;

                retval = reader.GetBytes(fieldnumber, startIndex, outbyte, 0, bufferSize);

                if (retval == 0)
                {
                    return("");
                }

                fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                bw = new BinaryWriter(fs);

                // Continue reading and writing while there are bytes beyond the size of the buffer.
                while (retval == bufferSize)
                {
                    bw.Write(outbyte);
                    bw.Flush();

                    // Reposition the start index to the end of the last buffer and fill the buffer.
                    startIndex += bufferSize;
                    retval      = reader.GetBytes(fieldnumber, startIndex, outbyte, 0, bufferSize);
                }

                // Write the remaining buffer.
                bw.Write(outbyte, 0, (int)retval);
                bw.Flush();

                // Close the output file.
                bw.Close();
                fs.Close();

                fileName = new FileInfo(fileName).FullName;
            }
            catch (IOException e)
            {
                throw e;
            }

            return(fileName);
        }