我参照了网上mssql的图片存取示例,并作了部分修改,存储正常,但显示的话就是老报错,显不出图片。请诸位帮看看。Response.OutputStream.Write((byte[])dr["photo"], 0, (int)dr["photolen"]); 在(byte[])dr["photo"]这里报“指定的转换无效”怀疑是数据库字段类型的问题,photo字段在mysql数据库中定义为“mediumblob”不知道这样对不对!
调试欢乐多
System.IO.FileStream fs = new System.IO.FileStream("file path and name", IO.FileMode.Open, IO.FileAccess.Read);
byte[] imgData = new byte[fs.Length];
fs.Read(imgData, 0, fs.Length);
SqlConnection conn = new SqlConnection("...");
SqlDataAdapter da = new SqlDataAdapter("select ... ", conn);
DataSet ds = new DataSet();
da.Fill(ds);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
ds.Tables[0].Rows[0]["img"] = imgData;
da.Update(ds);
请大家帮帮忙啊!
http://edu.tmn.cn/html/9/76/199/200533/3120.htm
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20030503122536HC2
----------------------------------------------------------------------
//为方便,写一个固定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();