zoukankan      html  css  js  c++  java
  • 保存图像到数据库

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;//流

    public partial class 保存图像到数据库_1_增加 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    //相片为图像:
    //一般可以在表中保存上传在服务器的文件名,
    //也可以把图像文件以二进制的形式保存在表中image类型的列中
    //现在来实现把文件保存到数据库中表的实现:过程分二步,
    //第一步:上传文件到服务器中,
    //第二步:把上传到服务器文件读成字节保存到表中

    //第一步:上传
    string fileName = DateTime.Now.ToString("yyyyMMdd-hhmmss")+".jpg";
    FileUpload1.SaveAs(Server.MapPath("~/temp/"+fileName));
    //第二步:保存到数据库
    Student stu = new Student();
    stu.Name = this.TextBox1.Text;
    stu.Sex = this.TextBox2.Text;
    //读文件字节数据
    FileStream fs = new FileStream(Server.MapPath("~/temp/" + fileName), FileMode.Open, FileAccess.Read);
    //定义字节数组
    byte[] bys = new byte[fs.Length];
    //读图片文件
    fs.Read(bys, 0, (int)fs.Length);
    fs.Close();
    //设置image类型列的值
    stu.Photo = new System.Data.Linq.Binary(bys);
    ///////////////////////////////////////////////
    DataClassesDataContext dataContext = new DataClassesDataContext();
    dataContext.Student.InsertOnSubmit(stu);
    //同步数据库
    dataContext.SubmitChanges();


    //删除上传的文件
    if (File.Exists(Server.MapPath("~/temp/" + fileName)))
    {
    File.Delete(Server.MapPath("~/temp/" + fileName));
    }

    Response.Write("<script>alert('增加成功');</script>");
    }
    }

  • 相关阅读:
    wget(转)
    852. Peak Index in a Mountain Array
    617. Merge Two Binary Trees
    814. Binary Tree Pruning
    657. Judge Route Circle
    861. Score After Flipping Matrix
    832. Flipping an Image
    461. Hamming Distance
    654. Maximum Binary Tree
    804. Unique Morse Code Words
  • 原文地址:https://www.cnblogs.com/panjuan/p/3811532.html
Copyright © 2011-2022 走看看