asp.net中如何从数据库中读出图像? asp.net中如何从数据库中读出图像?具体的语句是什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1 http://dotnet.aspx.cc/ShowDetail.aspx?id=9154BC99-DF64-4E2D-B096-26C99CE464BE 写入数据库//上传if(myFile.ContentLength != 0){ try { System.Web.HttpPostedFile myFile = this.Request.Files[0];// string tmpFileName = myFile.FileName;// string myFileName = tmpFileName.Substring(tmpFileName.LastIndexOf("."));// string myFileMimeType = myFile.ContentType();// myFile.SaveAs(this.Server.MapPath("../" + myFileName)); //读取到数组里面 System.IO.Stream mystream = myFile.InputStream; byte[] Buffer = new byte[myFile.ContentLength]; mystream.Read(Buffer,0,myFile.ContentLength); //打开数据库 OracleConnection cn = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]); cn.Open(); //用参数方式写入数据库 OracleCommand myComm = cn.CreateCommand(); string sql = "insert into tmp(tmp_id,tmp_blob) values(tmp_seq.nextval,:tmp_blob)"; myComm.CommandText = sql; myComm.Parameters.Add(":tmp_blob",OracleType.Blob,Buffer.Length).Value = Buffer; myComm.ExecuteNonQuery(); } catch { //此处可加错误显示 } finally { cn.Close(); }}在页面中,放一个Image控件,在后台指定它的链接地址如下:----------------------------------------------------------------------//为方便,写一个固定ID号tmp_id=103this.Image1.ImageUrl = "showimg.aspx?tmp_id=103" ;下面是显示图片的页面showimg.aspx后台代码,该页面不需要放任何东西.--------------------------------------------------------------------//创建数据库连接OracleConnection myConnection = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);myConnection.Open();//打开数据库OracleCommand myCommand = myConnection.CreateCommand();string sql = "select tmp_blob from tmp where tmp_id = 103";myCommand.CommandText = sql;OracleDataReader myRead = myCommand.ExecuteReader();//开始读取myRead.Read();//这个方法更直接Byte[] Buffer = (Byte[])myRead[0];//OracleLob myLob = myRead.GetOracleLob(0);//长度是long,转为int32//int myLength = Convert.ToInt32(myLob.Length); //Byte[] Buffer = new byte[myLength]; //myLob.Read(Buffer,0,myLength);//输出this.Response.Clear();//输出mime类型,根据上传的文件取到的mimetype,是什么我忘记了,自己写上,不过不写也没有关系.this.Response.ContentType = "";this.Response.BinaryWrite(Buffer);this.Response.End(); 楼上的都说了:从数据库中读出图片(二进制),在输出来(二进制)还可以把图片存在XML中(字符串),再转换成二进制输出来. http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=211573从数据库中读取IMG并生成图片 http://sunnystar365.cnblogs.com/archive/2005/10/10/251545.html W3WP 进程是否一直存在 怎么让解决方案生成.dll动态连接库文件 新网站无法运作(非常紧急,在线等候!!分数有限,只剩20~~) 求助!弹出窗口确定下一步执行流程问题 文件为什么不存在 ADO.Net 添加参数时不指定类型和长度有什么问题吗? 前台验证文本框输入 重复窗口问题 求助:关于<Identity>配置 JS 图片滚动效果做出来后点击图片的链接怎么弄 求一存储过程 问一个小问题Label1的!
if(myFile.ContentLength != 0)
{
try
{
System.Web.HttpPostedFile myFile = this.Request.Files[0];
// string tmpFileName = myFile.FileName;
// string myFileName = tmpFileName.Substring(tmpFileName.LastIndexOf("."));
// string myFileMimeType = myFile.ContentType();
// myFile.SaveAs(this.Server.MapPath("../" + myFileName)); //读取到数组里面
System.IO.Stream mystream = myFile.InputStream;
byte[] Buffer = new byte[myFile.ContentLength];
mystream.Read(Buffer,0,myFile.ContentLength); //打开数据库
OracleConnection cn = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);
cn.Open(); //用参数方式写入数据库
OracleCommand myComm = cn.CreateCommand();
string sql = "insert into tmp(tmp_id,tmp_blob) values(tmp_seq.nextval,:tmp_blob)";
myComm.CommandText = sql;
myComm.Parameters.Add(":tmp_blob",OracleType.Blob,Buffer.Length).Value = Buffer;
myComm.ExecuteNonQuery();
}
catch
{
//此处可加错误显示
}
finally
{
cn.Close();
}
}
在页面中,放一个Image控件,在后台指定它的链接地址如下:
----------------------------------------------------------------------
//为方便,写一个固定ID号tmp_id=103
this.Image1.ImageUrl = "showimg.aspx?tmp_id=103" ;
下面是显示图片的页面showimg.aspx后台代码,该页面不需要放任何东西.
--------------------------------------------------------------------
//创建数据库连接
OracleConnection myConnection = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);
myConnection.Open();//打开数据库
OracleCommand myCommand = myConnection.CreateCommand();
string sql = "select tmp_blob from tmp where tmp_id = 103";
myCommand.CommandText = sql;
OracleDataReader myRead = myCommand.ExecuteReader();//开始读取
myRead.Read();
//这个方法更直接
Byte[] Buffer = (Byte[])myRead[0];
//OracleLob myLob = myRead.GetOracleLob(0);
//长度是long,转为int32
//int myLength = Convert.ToInt32(myLob.Length);
//Byte[] Buffer = new byte[myLength];
//myLob.Read(Buffer,0,myLength);//输出
this.Response.Clear();
//输出mime类型,根据上传的文件取到的mimetype,是什么我忘记了,自己写上,不过不写也没有关系.
this.Response.ContentType = "";
this.Response.BinaryWrite(Buffer);
this.Response.End();
从数据库中读出图片(二进制),在输出来(二进制)
还可以把图片存在XML中(字符串),再转换成二进制输出来.
从数据库中读取IMG并生成图片