现在的位置: 首页 > 综合 > 正文

从数据库中读出图片,存入硬盘

2013年12月08日 ⁄ 综合 ⁄ 共 964字 ⁄ 字号 评论关闭
不多说了,把代码放上来
  1. SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123;database=am89resource");
  2.                 conn.Open();
  3.                 SqlCommand cmd = new SqlCommand("",conn);
  4.                 cmd.CommandText = "select * from picture where id='0331010009'";
  5.               
  6.                 SqlDataReader dr = cmd.ExecuteReader();
  7.                 if (dr.Read())
  8.                 {
  9.                     //byte[] file = (Byte[])dr["img"];
  10.                     //BinaryWriter bw = new BinaryWriter(fs);
  11.                     //bw.Write(file,0,file.Length);
  12.                     //Response.BinaryWrite(file);
  13.                     byte[] bytes = (byte[])dr["img"];
  14.                     FileStream fs = new FileStream(Server.MapPath("//img//1.gif"), FileMode.Create, FileAccess.Write);
  15.                     fs.Write(bytes, 0, bytes.Length);
  16.                     fs.Flush();
  17.                     fs.Close();
  18.                 }  
  19.                 conn.Close();

图片的1.gif你可以根据你的要求,另存为,可以采用时间做为名字来处理

抱歉!评论已关闭.