使用Long DataType读取存储在Oracle中的图像

时间:2012-02-14 12:58:48

标签: asp.net-mvc oracle

我想读取存储在Oracle Long数据类型中的图像。 图像数量存储在远程Oracle数据库的数据类型为long的列中。我只需要检索这些图像并在我的aspx页面上显示它们。 我可以从数据库中检索图像,但是当试图将其关闭到字节数组时,它会抛出错误,字符串无法转换为byte []'。 任何人都有关于如何检索存储在数据库中的长列中的这些图像的任何建议。

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"/> `

2 个答案:

答案 0 :(得分:1)

试试这个(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");
            }

此示例仅将Oracle中的长原始数据读入字节数组,然后输出到文件。注意InitalLONGFetchSize&gt; 0.

答案 1 :(得分:0)

我使用这个类:我的数据库是informix,图像存储在Byte类型。希望这可以帮到你。


 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;
        }
    }