问题描述
在index.aspx页中datalist控件中显示图片.图片以二进制形式存储在access数据库中.应该怎样写?
解决方案
解决方案二:
关注一下……
解决方案三:
有没有帮帮我啊?初学者的痛苦啊!
解决方案四:
帮帮忙啊?初学者的辛酸史啊!
解决方案五:
图片src指向单独的页面,这个页面根据参数输出图片
解决方案六:
不显示啊
解决方案七:
这是第一个页面的前台代码<asp:datalistid="MyList"repeatcolumns="2"borderwidth="0"runat="server"Height="91px"Width="621px"><ItemTemplate><table><tr><td><imgwidth="40"height="40"src='<%#DataBinder.Eval(Container.DataItem,"pID","Img.aspx?id={0}")%>'/></td></tr></table></ItemTemplate></asp:datalist>后台OleDbConnectionobjConn=newOleDbConnection();objConn.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=F:\2005ms\shiyan\house\house\fyfile\fyasp.mdb";objConn.Open();OleDbCommandobjCmd=newOleDbCommand("select*fromFileLibrary",objConn);OleDbDataAdapterda=newOleDbDataAdapter(objCmd);DataSetds=newDataSet();da.Fill(ds,"aa");MyList.DataSource=ds.Tables["aa"];MyList.DataBind();objConn.Close();img.aspx的后台代码intid=int.Parse(Request["id"].ToString());OleDbConnectionobjConn=newOleDbConnection();objConn.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=F:\2005ms\shiyan\house\house\fyfile\fyasp.mdb";objConn.Open();OleDbCommandobjCmd=newOleDbCommand("selectpid,ContentType,DatafromFileLibrarywherepID==@idorderbypIDdesc",objConn);objCmd.Parameters.Add("@id",OleDbType.Integer);objCmd.Parameters["@id"].Value=id;OleDbDataReaderobjDr=objCmd.ExecuteReader();while(objDr.Read()){Response.BinaryWrite((byte[])objDr["Data"]);}objDr.Close();objConn.Close();这样不显示啊不知道怎么回事
解决方案八:
/*图片二进制读取于存储到数据库这里用的是MYSQL数据库的BLOB字段存储的图片二进制数据由于用的是MYSQL数据库所以在引用存储的方法与MSSQL数据库不一样,MYSQL用的是“?”,而MSSQL数据库是“@”,请大家注意参考只要把MySql开头换成sql就行*/usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;usingSystem.IO;usingSystem.Drawing.Imaging;usingMySql.Data.MySqlClient;//引用MYSQL命名空间publicpartialclass_Default:System.Web.UI.Page{MySqlConnectionconn=newMySqlConnection(System.Configuration.ConfigurationManager.AppSettings["conmy"]);protectedvoidPage_Load(objectsender,EventArgse){if(!this.Page.IsPostBack){this.show();}}protectedvoidButton1_Click(objectsender,EventArgse){if(FileUpload1.HasFile){//文件扩展名stringEx=FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf('.')+1);if(Ex.ToLower()=="jpg"||Ex.ToLower()=="gif"){intImageSize=FileUpload1.PostedFile.ContentLength;//图片的大小stringImageType=this.FileUpload1.PostedFile.ContentType;//图片类型StreamImageStream=this.FileUpload1.PostedFile.InputStream;Byte[]ImageCount=newByte[ImageSize];//调用方法转化二进制数据intbt=ImageStream.Read(ImageCount,0,ImageSize);MySqlCommandcomm=newMySqlCommand("testpic",conn);comm.CommandType=CommandType.StoredProcedure;MySqlParameterpj=newMySqlParameter("?myimage",MySqlDbType.LongBlob,ImageCount.Length);//图片pj.Value=ImageCount;//给这字段赋值二进制数据comm.Parameters.Add(pj);MySqlParameterpname=newMySqlParameter("?imagename",MySqlDbType.VarChar,100);//图片名称pname.Value=FileUpload1.FileName;comm.Parameters.Add(pname);MySqlParameterpsize=newMySqlParameter("?imagesize",MySqlDbType.Int32);//图片名称psize.Value=ImageSize;comm.Parameters.Add(psize);conn.Open();comm.ExecuteNonQuery();conn.Close();this.Label1.Text="插入成功!";this.show();}else{this.Label1.Text="格式不正确!!!";}}else{this.Label1.Text="没有文件!!!";}}privatebyte[]ImageToByte(stringpath)//声名一个把图片转化到二进制数据的方法{FileStreambuffer=newFileStream(path,FileMode.Open,FileAccess.Read);BinaryReaderbr=newBinaryReader(buffer);byte[]image=br.ReadBytes((int)buffer.Length);returnimage;}privatevoidshow(){MySqlDataAdapterda=newMySqlDataAdapter("select*fromimgpic",conn);DataSetds=newDataSet();da.Fill(ds,"a");this.GridView1.DataSource=ds.Tables["a"].DefaultView;this.GridView1.DataKeyNames=newstring[]{"id"};this.GridView1.DataBind();}protectedvoidGridView1_SelectedIndexChanging(objectsender,GridViewSelectEventArgse){stringsid=this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();Session["id"]=sid;this.Label1.Text=sid;this.Image1.ImageUrl="jpg2.aspx";}}//读取图片usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.IO;usingSystem.Data.SqlClient;usingMySql.Data.MySqlClient;publicpartialclassDefault2:System.Web.UI.Page{MySqlConnectionconn=newMySqlConnection(System.Configuration.ConfigurationManager.AppSettings["conmy"]);protectedvoidPage_Load(objectsender,EventArgse){if(!this.Page.IsPostBack){this.show();}}publicvoidshow(){stringss=Request.QueryString["id"].ToString();strings3="select*fromimgpicwhereid="+ss;MySqlCommandcomm=newMySqlCommand(s3,conn);conn.Open();MySqlDataReaderdr=comm.ExecuteReader(CommandBehavior.CloseConnection);while(dr.Read()){Response.Clear();Response.C;Response.BinaryWrite((byte[])dr["myimg"]);//读取}Response.End();conn.Close();}}//注意图片显示要在另一张页面中//在你要显示的页面添加img控件src="显示你图片的页面";
解决方案九:
先写图片输入的接口,再在handler中写一个图片读的过程,最好用request.queryingstring,在页面中的imghref写入相应的字串就可以了