在oracle数据库中保存和显示图片

 

//保存图片到数据库//

  #region  保存图片到数据库
  /// <summary>
  /// 保存图片到数据库
  /// </summary>
  /// <param name="strXh">学号</param>
  /// <param name="strType">类型 (是否为修改相片)</param>
  private void SavePic(string strXh)
  {
   //定义byte类型变量
   if(Upload.PostedFile.FileName=="")
   {
    this.lblMessage.Text=" 提示:请选择要上传的学生相片";
    return;
   }
   this.FilePath=Upload.PostedFile.FileName;
   int len = Upload.PostedFile.ContentLength;
   byte[] myblob  = new byte[len];
   Upload.PostedFile.InputStream.Read (myblob, 0, len);
   OracleDataAdapter myoda;
   OracleCommand cmd;
   DataSet myds = new DataSet();
   DataTable mydt = new DataTable();
   DataRow mydr;
   OracleConnection conn = new OracleConnection(conDBOracle);
   string selesql;
   selesql = "SELECT * FROM photo WHERE xh='" + strXh + "'";
   cmd = new OracleCommand(selesql, conn);
   myoda = new OracleDataAdapter(selesql, conn);
   myoda.SelectCommand = cmd;
   myoda.Fill(myds, "PHOTO");
   if (myds.Tables[0].Rows.Count > 0)//修改照片
   {
    string updatesql = "UPDATE photo SET zp=:vPHOTO WHERE xh='" + strXh + "'";
    OracleCommand updatecmd = new OracleCommand(updatesql, conn);
    myoda.UpdateCommand = updatecmd;
    myoda.UpdateCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP");
    myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
    myoda.Fill(myds, "PHOTO");
    mydt = myds.Tables[0];
    mydr = mydt.Rows[0];
    string a = mydr["XH"].ToString();
    mydr.BeginEdit();
    mydr["XH"] = strXh;
    mydr["ZP"] = myblob;
    mydr.EndEdit();
    int i=myoda.Update(myds, "PHOTO");
    if(i>0)
    {
     conn.Close();
     conn.Dispose();
     myds.Clear();
     myds.Dispose();
     Response.Redirect("Main.aspx?StuNo="+strXh);
    }
    else
    {
     this.lblMessage.Text="提示:学生相片修改失败";
    }
   }
   else//上传新照片
   {
    string insertsql = "INSERT INTO photo (xh,zp) VALUES ('" + strXh + "',:vPHOTO)";
    OracleCommand insertcmd = new OracleCommand(insertsql, conn);
    myoda.InsertCommand = insertcmd;
    myoda.InsertCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP");
    myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
    myoda.Fill(myds, "PHOTO");
    mydt = myds.Tables[0];
    mydr = mydt.NewRow();
    mydr.BeginEdit();
    mydr["XH"] = strXh;
    mydr["ZP"] = myblob;
    mydr.EndEdit();
    mydt.Rows.Add(mydr);
    int i=myoda.Update(myds, "PHOTO");
    if(i>0)
    {
     conn.Close();
     conn.Dispose();
     myds.Clear();
     myds.Dispose();//释放资源
     Response.Redirect("Main.aspx?StuNo="+strXh);
    }
   }
  }
  #endregion

/显示图片/

#region 显示学生照片
  /// <summary>
  /// 显示学生照片
  /// </summary>
  /// <param name="Xh">学生学号</param>
  private void ShowPic(string Xh)
  {
   if(Xh=="")//学号等于空时加一张空白图片
   {
    string strFilePath= Server.MapPath(".")+"//images//"+"nopic.gif";
    System.IO.FileStream fs=new FileStream(strFilePath,System.IO.FileMode.Open,System.IO.FileAccess.Read);
    Int32 len=(Int32)fs.Length;
    byte[] bytephoto = new byte[len];
    //bytephoto = (byte[])(myphotods.Tables[0].Rows[0]["ZP"]);
    fs.Read(bytephoto,0,len);
    MemoryStream stmPhoto = new MemoryStream(bytephoto);   
    System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";//这里须要改进
    //将文件保存在系统指定的目录下,从而方便导入到Excle中
    System.Drawing.Image image=System.Drawing.Image.FromStream(stmPhoto);
    System.Drawing.Image _newimage = image.GetThumbnailImage( 100, 200, null, new System.IntPtr());
    _newimage.Save( System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg );//这里须要改进
    fs.Close();
    stmPhoto.Close();//关闭
   }   
   OracleDataAdapter oraAda;
   DataSet myphotods = new DataSet();
   OracleCommand cmd;
   OracleConnection Con = new OracleConnection(conDBOracle);
   string strSql;
   strSql = "SELECT * FROM photo WHERE xh='" +Xh + "'";
   cmd = new OracleCommand(strSql, Con);
   oraAda = new OracleDataAdapter(strSql, Con);
   oraAda.SelectCommand = cmd;
   try
   {
    Con.Open();
    oraAda.Fill(myphotods, "PHOTO");
    if (myphotods.Tables[0].Rows.Count > 0)
    {
     byte[] bytephoto = new byte[0];
     bytephoto = (byte[])(myphotods.Tables[0].Rows[0]["ZP"]);
     MemoryStream stmPhoto = new MemoryStream(bytephoto);   
     System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";//这里须要改进
     //将文件保存在系统指定的目录下,从而方便导入到Excle中
     System.Drawing.Image image=System.Drawing.Image.FromStream(stmPhoto);
     System.Drawing.Image _newimage = image.GetThumbnailImage( 100, 200, null, new System.IntPtr());
     _newimage.Save( System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg );//这里须要改进
     stmPhoto.Close();//关闭
     this.SaveStuPic(Xh);
    }
    else//如果学生没有相片,添加一个临时图片
    {
     string strFilePath= Server.MapPath(".")+"//images//"+"nopic.gif";
     System.IO.FileStream fs=new FileStream(strFilePath,System.IO.FileMode.Open,System.IO.FileAccess.Read);
     Int32 len=(Int32)fs.Length;
     byte[] bytephoto = new byte[len];
     //bytephoto = (byte[])(myphotods.Tables[0].Rows[0]["ZP"]);
     fs.Read(bytephoto,0,len);
     MemoryStream stmPhoto = new MemoryStream(bytephoto);   
     System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";//这里须要改进
     //将文件保存在系统指定的目录下,从而方便导入到Excle中
     System.Drawing.Image image=System.Drawing.Image.FromStream(stmPhoto);
     System.Drawing.Image _newimage = image.GetThumbnailImage( 100, 200, null, new System.IntPtr());
     _newimage.Save( System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg );//这里须要改进
     fs.Close();
     stmPhoto.Close();//关闭
    }
   }
   catch(Exception err)
   {
    Response.Write(err.ToString());
    
   }
   finally
   {
    myphotods.Dispose();
    myphotods.Clear();
    Con.Close();
    Con.Dispose();
    oraAda.Dispose();
    GC.Collect();
   } 
  }
  #endregion  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值