【问题标题】:Read Image stored in Oracle using Long DataType使用 Long DataType 读取存储在 Oracle 中的图像
【发布时间】:2012-03-05 20:14:04
【问题描述】:

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

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

【问题讨论】:

  • 您是否尝试过从 Long 类型转换为 Lob 类型(更改表格本身)?长类型在 Oracle 中已弃用,应该是用于存储图像的 blob。
  • 感谢您的回复,但我无法更改表结构。这是一个远程数据库,我无权这样做。我只需要从他们的数据库中获取数据并显示出来。
  • 这似乎与this question 非常相似。
  • 是的,亚历克斯,但即使是那个问题也没有答案。
  • 嗨。你有没有找到任何解决方案?我被困在同样的情况下。我也在尝试从 Finacle CBS 获取我的数据。

标签: asp.net-mvc oracle


【解决方案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 > 0。

【讨论】:

  • 感谢您的帮助,但我获取的列是 LONG 数据类型,而不是 LONG RAW。我尝试了您的解决方案,但在我的场景中没有用。
  • @ABC 您将图像存储在 LONG 列中?
  • 不是我,我只是从远程数据库中获取数据,长列中的数据是由其他人存储的。这就是我无法修改表结构并将 LONG 列更改为 BLOB 或其他内容的原因。
  • @ABC 我相信只有字符数据(而不是二进制数据)应该存储在 LONG 列中。对于新模式,LONG 映射到 CLOB,LONG RAW 映射到 BLOB。来自 Oracle:“定义为 LONG 的列可以存储包含最多 2 GB 信息的可变长度字符数据。LONG 数据是在不同系统之间移动时要适当转换的文本数据”。见docs.oracle.com/cd/B28359_01/server.111/b28318/…
  • @ABC 是否有其他程序或应用程序正确使用此数据?我几乎总是处理 BLOB,所以我很惊讶图像是 LONG 而不是 LONG RAW
【解决方案2】:

我使用这个类:我的数据库是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;
        }
    }

【讨论】:

  • 感谢您的帮助,但是当我尝试将其转换为 byte[] 时,它会引发错误 - 无法将“System.String”类型的对象转换为“System.Byte[]”类型。当图像在数据库中存储为 Blob 时,它可以工作,但在我的情况下,图像存储为 LONG。
猜你喜欢
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
相关资源
最近更新 更多