如何使用 C# Web 应用程序从 Oracle 数据库中检索图像

本文关键字:数据库 检索 图像 Oracle 何使用 Web 应用程序 | 更新日期: 2023-09-27 18:36:08

我想在 asp.net 中将图像从 Oracle 数据库检索到Image控件。我试过了,但它不起作用。

这是用于将图像插入数据库的代码:

protected void btnUpload_Click(object sender, EventArgs e)
{
        int imgLength = 0;
        string imgContentType = null;
        string imgFileName = null;
        Stream imgStream = FileUpload.PostedFile.InputStream;
        imgLength = FileUpload.PostedFile.ContentLength;
        imgContentType = FileUpload.PostedFile.ContentType;
        imgFileName = FileUpload.PostedFile.FileName;
        if (imgContentType == "image/jpeg" || imgContentType == "image/gif" ||
        imgContentType == "image/pjpeg"
          || imgContentType == "image/bmp")
         {
            OracleConnection DbConnection = new OracleConnection(con1);
            DbConnection.Open();
            FileStream fls;
            fls = new FileStream(@imgFileName, FileMode.Open, FileAccess.Read);
            byte[] blob = new byte[fls.Length];
            fls.Read(blob, 0, System.Convert.ToInt32(fls.Length));
            fls.Close();
            string query = "insert into image(id,name,photo) values(1,'" + imgFileName + "'," + " :BlobParameter )";
            // Establish a new OracleCommand
            OracleCommand cmd = new OracleCommand();
            cmd.CommandText = query;
            cmd.Connection = DbConnection;
            cmd.CommandType = CommandType.Text;
            System.Data.OracleClient.OracleParameter paramImage = new System.Data.OracleClient.OracleParameter("image",
              Oracle.DataAccess.Client.OracleDbType.Blob);
            paramImage.ParameterName = "BlobParameter";
            paramImage.Value = blob;
            paramImage.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(paramImage);
            cmd.ExecuteNonQuery();
}

桌子:

  Id      Name                                 Photo
   1      C:''user'pictures'animal.jpeg        (BLOB)

下面是用于将图像检索到 image 控件中的代码,但此代码不起作用。在过去的两天里,我一直在为此苦苦挣扎

void GetImagesFromDatabase()
{
        try
        {
            OracleConnection DbConnection = new OracleConnection(con1);
            DbConnection.Open();
            OracleCommand cmd = new OracleCommand("Select name,photo from Image", DbConnection);
            OracleDataReader oda = cmd.ExecuteReader();
            while (oda.Read())
            {
                string path = oda[0].ToString();
                img.ImageUrl = path;
                if(oda.GetValue(1).ToString() !=""){
                    FileStream fls;
                    fls = new FileStream(@path, FileMode.Open, FileAccess.Read);
                    byte[] blob = new byte[fls.Length];
                    fls.Read(blob, 0, System.Convert.ToInt32(fls.Length));
                    fls.Close();
                    MemoryStream memStream = new MemoryStream(blob);
                    img.ImageUrl = oda[2].ToString();
                }
            }
        }
        catch (Exception ex)
        {
        }
}

有什么想法吗?提前致谢

如何使用 C# Web 应用程序从 Oracle 数据库中检索图像

也许这段代码可以帮助你:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}