简体   繁体   中英

How to retrieve image from Oracle database using C# web application?

I want to retrieve an image from an Oracle database to an Image control in asp.net. I tried but it's not working.

This is the code used for inserting image into database:

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

Table:

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

Below is the code used to retrieve the image into an image control but this code is not working. For the past two days I've been struggling with this

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)
        {
        }
}

Any ideas? Thanks in advance

maybe this code can help you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM