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>");
    }
    }

  • 相关阅读:
    Zookeeper数据类型
    Zookeeper基本命令
    Redis集群
    Mysql 模拟自增主键
    git回滚版本操作
    Redis缓存穿透和雪崩
    日期格式jackson格式化
    Zookeeper安装
    redis主从复制
    Redis哨兵模式
  • 原文地址:https://www.cnblogs.com/panjuan/p/3811532.html
Copyright © 2011-2022 走看看