Create a file from an OracleBFile

1

I have files saved in my Oracle database with field BFile . I do a select and I get that field BFile . But now I have doubts about how to get that byte array, recover the stored file and save it in C:\ .

These are my advances:

OracleCommand command = new OracleCommand("SELECT IMG FROM IMAGES WHERE ID='" + idText + "'", conn2); 
OracleDataReader reader = command.ExecuteReader();
using (reader)
{
    if (reader.Read())
    {

        OracleBFile bFile = reader.GetOracleBFile(0);
         byte[] buffer = new byte[bFile.Length];
         using (bFile)
         {
             bFile.Seek(0, SeekOrigin.Begin);
             bFile.Write(buffer, 0, 100);
         }
    }
    else
    {
        MessageBox.Show("Error");
    }
}
    
asked by linux mint 24.10.2016 в 23:23
source

1 answer

0

Using your example, let's say you want to store the contents of BFILE to c:\test\archivo.dat , this would be an easy way to achieve it:

using (var bFile = reader.GetOracleBFile(0))
{
    bFile.OpenFile();

    using (var fileStream = new FileStream(@"c:\test\archivo.dat", FileMode.Create))
    {
        bFile.CopyTo(fileStream);
    }
}

Additional note : I suggest that the instance of OracleCommand accept idText as a parameter rather than concatenate it directly into your SQL statement. Developing that good habit will avoid many problems.

    
answered by 25.10.2016 в 04:22