1

I want to read the image stored in Oracle Long datatype. Number of images are stored in a remote Oracle database in a column with datatype long. I just need to retrieve those images and show them on my aspx page. I could retrieve the image from database but when tried to caste it to byte array, it threw error that, string can not be converted to byte[]'. Anybody have any suggestions on how to retrieve these images stored in long column in database.

byte[] signatureBlobReceived = cls_TBL_BROKER_BL.GetInstance().GetSignatureBlobFromAccountNumber_BL(strCRNnumber);
 return File(signatureBlobReceived, "image/jpeg");


public byte[] GetSignatureBlobFromAccountNumber_BL()
{
object SignatureBlob = null;
Database db = DatabaseFactory.CreateDatabase("imageConnectionString");
DbCommand dbc = db.GetSqlStringCommand(ConfigurationSettings.AppSettings["signqry"].ToString());
dbc.CommandType = CommandType.Text;
SignatureBlob = db.ExecuteScalar(dbc);
byte[] array = Encoding.ASCII.GetBytes(Convert.ToString(SignatureBlob));
 string aa = string.Empty;
return array;
}

Query used is:
<add key="signqry" value="SELECT image FROM table1"/> `
tereško
  • 56,151
  • 24
  • 92
  • 147
ABC
  • 161
  • 1
  • 5
  • 16
  • have u tried converting from Long to Lob type (alter the table itself)? Long types are deprecated in Oracle, should be a blob for storing images. – tbone Feb 14 '12 at 13:00
  • Thanks for the response but I can not alter the table structure. It is a remote database & I don't have rights to do this. I just have to fetch the data from their database and display it. – ABC Feb 14 '12 at 15:36
  • This seems quite similar to [this question](http://stackoverflow.com/questions/9267525/read-image-stored-in-oracle-long-datatype). – Alex Poole Feb 14 '12 at 23:31
  • Yes Alex, but even that question is not answered yet. – ABC Feb 15 '12 at 07:42
  • Hi. Have you found any solution to this? I am stuck in the same case. I too am trying to fetch my data from a Finacle CBS. – Raxak Jan 09 '18 at 09:17

2 Answers2

1

Try this (odp.net)

            string connStr = "User Id=user;Password=pwd;Data Source=mySID;";
            OracleConnection _conn = new OracleConnection(connStr);
            _conn.Open();

            string sel = @"select long_raw_col from long_raw_test";
            OracleCommand cmd = new OracleCommand(sel, _conn);
            cmd.InitialLONGFetchSize = 5000;
            OracleDataReader reader = cmd.ExecuteReader();

            int rows = 0;
            // loop through rows from table
            while (reader.Read())
            {
                rows++;
                byte[] buf = new byte[5000];
                long bytesRead = reader.GetBytes(reader.GetOrdinal("long_raw_col"), 0, buf, 0, 5000);
                FileStream fs = new FileStream("C:\\test\\test_long" + rows + ".dat", FileMode.Create);
                fs.Write(buf, 0, (int)bytesRead);
                fs.Close();

                Console.WriteLine("Row " + rows + ": Read " + bytesRead + " bytes from table, see test_long" + rows + ".dat");
            }

This example just reads the long raw data from Oracle into a byte array, then outputs to a file. Note the InitalLONGFetchSize > 0.

tbone
  • 13,914
  • 3
  • 31
  • 39
  • 1
    Thanks for the help, but the column that I am fetching is of datatype LONG and not LONG RAW. I tried your solution but its of no use in my scenario. – ABC Feb 15 '12 at 07:46
  • @ABC you are storing images in a LONG column? – tbone Feb 15 '12 at 12:06
  • Not me, I am just fetching the data from a remote database, the data in long column is stored by someone else. That is the reason I can't modify the table structure and change the LONG column to BLOB or something else. – ABC Feb 15 '12 at 12:12
  • @ABC I believe only character data (not binary) should be stored in LONG columns. For new schemas, LONG maps to CLOB, and LONG RAW maps to BLOB. From Oracle: "Columns defined as LONG can store variable-length character data containing up to 2 gigabytes of information. LONG data is text data that is to be appropriately converted when moving among different systems". See http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#i3056 – tbone Feb 15 '12 at 12:15
  • @ABC is any other program or app using this data correctly? I almost always deal with BLOBs so I'm surprised images are in LONG and not LONG RAW – tbone Feb 15 '12 at 12:16
  • @ABC also, when u tried my code, what did the output file look like? Try removing "test_long.dat" and run again, but only select 1 row (1 image) from your select statement. – tbone Feb 15 '12 at 12:20
  • @ABC updated code to output 1 file per image. This should work on LONG and LONG RAW columns. Let me know how the output image files look. Also, increase fetch size (I used 5000) if grabbing larger images. – tbone Feb 15 '12 at 13:00
  • Actually, this data is to be fetched from a core banking solution system (CBS) ,and our application is a product used by the bank which will access the data of CBS. So obviously the CBS is already using those images ,but we cant ask them how do they do it. When I tried your solution, it is giving me error, no data found ,even though the data is there. – ABC Feb 17 '12 at 09:30
  • @ABC I can't debug your app for you, but I assume you mean C# is throwing "Column contains NULL data" error(?). Obviously the assumption here is that your are selecting rows that actually have image data. Your comment re:"no data found" refers an Oracle error when selecting into a variable, not a C# error that I know. Setup a test table and insert some test data, then try my approach. If you have an issue, be specific and show work so I can follow you. – tbone Feb 17 '12 at 11:28
  • @tbone thanx for the answer.. it works perfect for long raw data type.. for retriving single image, jut change the extension from .dat to .jpg and assign it to object type image.. – Pankaj Nagarsekar Oct 03 '13 at 09:56
0

I use this class :my database is informix and the images are stored in Byte type .Hope this can help you.


 public class MyPhoto
    {
        public static Stream RetrievePhoto()
        {
            DBConnection DAL_Helper = new DBConnection(ConfigurationSettings.AppSettings["connection"].ToString());
            Byte[] myByteBuff;
            Stream myImgStream;
            string qry = "----------";
            DataTable dt = DAL_Helper.Return_DataTable(qry);
            try
            {
                if (dt.Rows.Count > 0)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                    {
                        myByteBuff = (Byte[])((object)(dt.Rows[0][0]));
                        myImgStream = new MemoryStream(myByteBuff);
                    }
                    else
                    {
                        myImgStream = RetrievePhotoNoProfile();
                    }
                }
                else
                {
                    myImgStream = RetrievePhotoNoProfile();
                }
            }
            catch (Exception ex)
            {
                myImgStream = RetrievePhotoNoProfile();
            }
            return myImgStream;
        }

        public static byte[] StreamToByteArray(Stream stream)
        {
            if (stream is MemoryStream)
            {
                return ((MemoryStream)stream).ToArray();
            }
            else
            {
                return ReadFully(stream);
            }
        }
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[input.Length];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        private static Stream RetrievePhotoNoProfile()
        {
            string noprofileimgPath = HttpContext.Current.Server.MapPath("~/images/noprofile.png");
            System.IO.FileStream fs = new System.IO.FileStream(noprofileimgPath, System.IO.FileMode.Open, FileAccess.Read);
            byte[] ba = new byte[fs.Length];
            fs.Read(ba, 0, (int)fs.Length);
            Stream myImgStream = new MemoryStream(ba);
            fs.Close();
            return myImgStream;
        }

        public static Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
    }
Anyname Donotcare
  • 10,000
  • 54
  • 200
  • 355
  • 1
    Thanks for the help, but when I try to cast it in byte[], it throws error- Unable to cast object of type 'System.String' to type 'System.Byte[]'. It works when the image is stored as Blob in database, but in my case the image is stored as LONG. – ABC Feb 14 '12 at 15:50