zoukankan      html  css  js  c++  java
  • winform把图片存储到数据库

    1、先在Form中放一个PictureBox控件,再放三个按钮。

    2、双击打开按钮,在里面写如下代码:

    OpenFileDialog open1 = new OpenFileDialog();
                DialogResult isok = open1.ShowDialog();
                if (isok == DialogResult.OK)
                {
                    string filename = open1.FileName;
                    FileStream fs = new FileStream(filename,FileMode.Open);
                    Image img = Image.FromStream(fs);
                    pictureBox1.Image = img;
                    fs.Close();
                }

    3、双击存储到数据库按钮,在里面写如下代码:

    OpenFileDialog open1 = new OpenFileDialog();
                DialogResult isok = open1.ShowDialog();
                if (isok == DialogResult.OK)
                {
                    //根据路径读取图片,把图片存到数据库
                    string filename = open1.FileName;
                    FileStream fs = new FileStream(filename, FileMode.Open);
                    BinaryReader br = new BinaryReader(fs);//二进制读取器
                    byte[] buffer = br.ReadBytes((int)fs.Length);//图片数据进去到buffer中
                    br.Close();
                    fs.Close();
                    SqlConnection conn = new SqlConnection("server=.;database=图片;user=sa;pwd=123");
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into tupian values(@image)";
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@image", buffer);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }

    4、双击读取数据库按钮,在里面写如下代码:

    SqlConnection conn = new SqlConnection("server=.;database=图片;user=sa;pwd=123");
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select *from tupian where id=1";
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    byte[] buffer = (byte[])dr["image"];
                    MemoryStream ms = new MemoryStream(buffer);
                    Image img = Image.FromStream(ms);
                    pictureBox1.Image = img;
                }
                conn.Close();
  • 相关阅读:
    部署openstack的官网文档解读mysql的配置文件
    ubuntu14.04行更新软件包
    Ubuntu14.04上修改主机名
    ubuntu上修改root密码
    在ISE查看各个模块消耗的资源
    132
    Aurora 8B/10B、PCIe 2.0、SRIO 2.0三种协议比较
    NAND flash和NOR flash的区别详解
    FPGA三分频,五分频,奇数分频
    以太网之物理层
  • 原文地址:https://www.cnblogs.com/jiangshuai52511/p/5341240.html
Copyright © 2011-2022 走看看