protected void Button1_Click(object sender, EventArgs e) { string sConn = ConfigurationManager.AppSettings["ConnectionString"]; SqlConnection objConn = new SqlConnection(sConn); objConn.Open(); string sql = "select * from Images"; SqlCommand cmd = new SqlCommand(sql, objConn); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { byte[] bytes = (byte[])dr["img"]; FileStream fs = new FileStream(@"E:\Images\" + dr["roll_no"] + ".jpg" , FileMode.Create, FileAccess.Write); fs.Write(bytes, 0, bytes.Length); fs.Flush(); fs.Close(); } dr.Close(); objConn.Close(); Response.Write("成功导出"); }
运行结果:
点击“导出”:
打开指定的文件夹,图片已经保存在里面了:
最后,如果有需要,你还可以参考这篇文章:如何保存PDF、Word和Excel文件到数据库中