Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hii,
i am trying to display image from Blob datatype of oracle10g in .net windows application.
i am using filestream to get the image but the when i set the steam object for display image it show the error (Cannot access a closed file).

My code is below.

OracleConnection con = new OracleConnection("Data Source=localhost;uid=test;pwd=test;");
            con.Open();
            try
            {
                OracleCommand cmd = new OracleCommand("Select Token_no,Photo from party_photo where token_no=8629", con);
                // Writes the BLOB to a file (*.bmp).
                FileStream stream;
                // Streams the BLOB to the FileStream object.
                BinaryWriter writer;

                // Size of the BLOB buffer.
                int bufferSize = 100;
                // The BLOB byte[] buffer to be filled by GetBytes.
                byte[] outByte = new byte[bufferSize];
                // The bytes returned from GetBytes.
                long retval;
                // The starting position in the BLOB output.
                long startIndex = 0;

                // The publisher id to use in the file name.
                string token_no = "";

                OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                while (reader.Read())
                {
                    //token_no = reader.GetString(0);
                    token_no = Convert.ToString(reader[0]);
                    stream = new FileStream("Photo" + token_no + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
                    writer = new BinaryWriter(stream);
                    startIndex = 0;
                    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
                    while (retval == bufferSize)
                    {
                        writer.Write(outByte);
                        writer.Flush();

                        startIndex += bufferSize;
                        retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);

                    }
                    writer.Write(outByte, 0, (int)retval - 1);
                    writer.Flush();

                    //stream.Write(outByte, 0, (int)retval = 1);
                    writer.Close();
                    pictureBox1.Image  = Image.FromStream(stream);
                    stream.Close();
                    
                }
                reader.Close();
                



            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, "", MessageBoxButtons.OK);
            }
            finally
            {
                con.Close();
            }


Please help me to short out this problem.


Thanks
Posted
Updated 16-Jul-11 17:37pm
v2

Switching to ODAC[^] is probably a good idea, as MS has deprecated their oracle ado.net driver.

You'll also get access to functionality that is better suited for retreiving oracle blob types.

Best regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900