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

1.数据库表结构

 1 None.gif if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[Person] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
 2 None.gif drop   table   [ dbo ] . [ Person ]
 3 None.gif GO
 4 None.gif
 5 None.gif CREATE   TABLE   [ dbo ] . [ Person ]  (
 6 None.gif     [ PersonID ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
 7 None.gif     [ PersonImage ]   [ image ]   NULL  ,
 8 None.gif     [ PersonImageType ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  
 9 None.gif ON   [ PRIMARY ]  TEXTIMAGE_ON  [ PRIMARY ]
10 None.gif GO
11 None.gif
12 None.gif

2.显示图片的代码,把下面的代码随便放到一个aspx页面的pageload事件中

 1 None.gif      void  GetImageSrcFromDB()
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            string strImageID = Request.QueryString["id"];
 4InBlock.gif            SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=sa;");
 5InBlock.gif            SqlCommand myCommand = new SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID=" 
 6InBlock.gif                + strImageID, myConnection);
 7InBlock.gif
 8InBlock.gif            try
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
10InBlock.gif                myConnection.Open();
11InBlock.gif                SqlDataReader myDataReader;
12InBlock.gif                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
13InBlock.gif                if(myDataReader.Read())
14ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
15InBlock.gif                    Response.Clear();
16InBlock.gif
17InBlock.gif                    //Response.ContentType = myDataReader["PersonImageType"].ToString();
18InBlock.gif                    Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
19ExpandedSubBlockEnd.gif                }

20InBlock.gif                myConnection.Close();
21ExpandedSubBlockEnd.gif            }

22InBlock.gif            catch (SqlException SQLexc)
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                Response.Write(SQLexc.ToString());
25ExpandedSubBlockEnd.gif            }

26InBlock.gif            //Response.End();
27ExpandedBlockEnd.gif        }

3.构造数据,通过上传的方式把图片存入数据库,下面的代码放入一个上传按钮的单击事件中,页面上在加一个上传控件

 1 None.gif              // 获得图象并把图象转换为byte[] 
 2 None.gif             HttpPostedFile upPhoto = UpPhoto.PostedFile; 
 3 None.gif             int  upPhotoLength = upPhoto.ContentLength; 
 4 None.gif             byte [] PhotoArray = new  Byte[upPhotoLength]; 
 5 None.gif            Stream PhotoStream = upPhoto.InputStream; 
 6 None.gif            PhotoStream.Read(PhotoArray, 0 ,upPhotoLength); 
 7 None.gif        
 8 None.gif             // 连接数据库 
 9 None.gif             SqlConnection conn = new  SqlConnection(); 
10 None.gif            conn.ConnectionString = " Data Source=localhost;Database=mxh;User Id=sa;Pwd=sa "
11 None.gif            
12 None.gif             string  strSql = " Insert into Person(PersonImage,PersonImageType) values(@FImage,'jpeg') "
13 None.gif            SqlCommand cmd = new  SqlCommand(strSql,conn); 
14 None.gif            cmd.CommandType = CommandType.Text ; 
15 None.gif            
16 None.gif             // 如果你希望不使用存储过程来添加图片把上面四句代码改为: 
17 None.gif            
18 None.gif             // SqlCommand cmd=new SqlCommand(strSql,conn); 
19 None.gif             cmd.Parameters.Add( " @FImage " ,SqlDbType.Image); 
20 None.gif            cmd.Parameters[ " @FImage " ].Value = PhotoArray; 
21 None.gif            conn.Open(); 
22 None.gif            cmd.ExecuteNonQuery(); 
23 None.gif            conn.Close(); 





4.显示图片的代码,建立一个新的aspx页面,然后显示图片

1 None.gif < asp:Image  id ="Image1"  runat ="server"  ImageUrl ="ReadImage.aspx?id=1" ></ asp:Image >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值