Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#web应用程序从Oracle数据库检索图像?_C#_Asp.net_Image_Oracle - Fatal编程技术网

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

如何使用C#web应用程序从Oracle数据库检索图像?,c#,asp.net,image,oracle,C#,Asp.net,Image,Oracle,我想从Oracle数据库检索一个图像到asp.net中的image控件。我试过了,但没用 这是用于将图像插入数据库的代码: protected void btnUpload_Click(object sender, EventArgs e) { int imgLength = 0; string imgContentType = null; string imgFileName = null; Stream imgStream =

我想从Oracle数据库检索一个图像到asp.net中的
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)
        {
        }
}

有什么想法吗?提前感谢

也许此代码可以帮助您:

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

也许此代码可以帮助您:

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

ImageUrl
只是一个指向图像的URL。您不能仅将字节流分配给该url。。。。。使用HttpHandler返回图像的方法,
ImageUrl
就是这样一种指向图像的URL。您不能仅将字节流分配给该url。。。。。有关使用HttpHandler返回图像的方法