45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:如何在oracle数据库中保存和显示图片?

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

2016-09-04 09:25:46 来源:www.45fan.com 【

如何在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

 

本文地址:http://www.45fan.com/a/question/72132.html
Tags: oracle 数据库 保存
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部