如何从oracle数据库中检索图像?

时间:2013-10-17 11:44:57

标签: c# asp.net image oracle

我想从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:\Document\Image\Ocean.jpeg   (BLOB) 

在下面的代码中,我试图检索并在图像控件中显示该图像 但它不起作用

检索代码:

 void GetImagesFromDatabase()
    {
        try
        {
            OracleConnection DbConnection = new OracleConnection(con1);
            DbConnection.Open();
            OracleCommand cmd = new OracleCommand("Select name from Image", DbConnection);
            OracleDataReader oda = cmd.ExecuteReader();

            while (oda.Read())
            {
                string path = oda[0].ToString();
                img.ImageUrl = path;
            }
        }
        catch (Exception ex)
        {

        }
    }

有什么想法吗?提前致谢

0 个答案:

没有答案