如何显示数据库中Image类型的图片

    技术2022-05-11  58

    1.数据库表结构  1 if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[Person] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )  2 drop   table   [ dbo ] . [ Person ]  3 GO  4  5 CREATE   TABLE   [ dbo ] . [ Person ]  (  6      [ PersonID ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,  7      [ PersonImage ]   [ image ]   NULL  ,  8      [ PersonImageType ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL    9 ON   [ PRIMARY ]  TEXTIMAGE_ON  [ PRIMARY ] 10 GO 11 12 2.显示图片的代码,把下面的代码随便放到一个aspx页面的pageload事件中  1      void  GetImageSrcFromDB()  2          { 3            string strImageID = Request.QueryString["id"]; 4            SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=sa;"); 5            SqlCommand myCommand = new SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID="  6                + strImageID, myConnection); 7 8            try 9            {10                myConnection.Open();11                SqlDataReader myDataReader;12                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);13                if(myDataReader.Read())14                {15                    Response.Clear();1617                    //Response.ContentType = myDataReader["PersonImageType"].ToString();18                    Response.BinaryWrite((byte[])myDataReader["PersonImage"]);19                }20                myConnection.Close();21            }22            catch (SqlException SQLexc)23            {24                Response.Write(SQLexc.ToString());25            }26            //Response.End();27        } 3.构造数据,通过上传的方式把图片存入数据库,下面的代码放入一个上传按钮的单击事件中,页面上在加一个上传控件  1              // 获得图象并把图象转换为byte[]   2             HttpPostedFile upPhoto = UpPhoto.PostedFile;   3              int  upPhotoLength = upPhoto.ContentLength;   4              byte [] PhotoArray = new  Byte[upPhotoLength];   5             Stream PhotoStream = upPhoto.InputStream;   6             PhotoStream.Read(PhotoArray, 0 ,upPhotoLength);   7           8              // 连接数据库   9             SqlConnection conn = new  SqlConnection();  10             conn.ConnectionString = " Data Source=localhost;Database=mxh;User Id=sa;Pwd=sa " 11              12              string  strSql = " Insert into Person(PersonImage,PersonImageType) values(@FImage,'jpeg') " 13             SqlCommand cmd = new  SqlCommand(strSql,conn);  14             cmd.CommandType = CommandType.Text ;  15              16              // 如果你希望不使用存储过程来添加图片把上面四句代码改为:  17              18              // SqlCommand cmd=new SqlCommand(strSql,conn);  19             cmd.Parameters.Add( " @FImage " ,SqlDbType.Image);  20             cmd.Parameters[ " @FImage " ].Value = PhotoArray;  21             conn.Open();  22             cmd.ExecuteNonQuery();  23             conn.Close();  4.显示图片的代码,建立一个新的aspx页面,然后显示图片 1 < asp:Image  id ="Image1"  runat ="server"  ImageUrl ="ReadImage.aspx?id=1" ></ asp:Image >  

    最新回复(0)