1

我想从 oracle 数据库中检索图像

并在我尝试过的图像控件中显示它,但它显示的是空图像

插入图像的代码:

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

        }
    }

有任何想法吗?提前致谢

4

0 回答 0