• winform图片读取存储于数据库SQL


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    
    namespace WindowsFormsApplication10
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "@.Jpg|*.jpg|@.Gif|*.gif|@.Png|*.png|All files|*.*";
                DialogResult dr = openFileDialog1.ShowDialog();
    
                if (dr == DialogResult.OK)
                {
                    //将图片读入到文件流
                    FileStream fs = new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);//路径
                    Image img = System.Drawing.Bitmap.FromStream(fs);//绘制,流转成图片
                    pictureBox1.Image = img;//指定,显示图片
                    fs.Close();
                  
                }
            }
    
            private void button2_Click(object sender, EventArgs e)//存入数据库
            {
                openFileDialog1.Filter = "@.Jpg|*.jpg|@.Gif|*.gif|@.Png|*.png|All files|*.*";
                DialogResult dr = openFileDialog1.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    //文件流
                    FileStream fs = new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);//二进制读取器
                    byte[] buffer =  br.ReadBytes(int.Parse(fs.Length.ToString()));
    
                    //连接数据库
                    SqlConnection conn = new SqlConnection("server=.;database=snewData;user=sa;pwd=");
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into imgtable values(@buffer)";
                    cmd.Parameters.Add("@buffer",buffer);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    conn.Close();
                }
            }
    
            private void button3_Click(object sender, EventArgs e)//数据库读取
            {
                //读取数据库
                byte[] buffer = null;
                SqlConnection conn = new SqlConnection("server=.;database=snewData;user=sa;pwd=");
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select *from imgtable where code=4";
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                     buffer = (byte[])dr["imgs"];
                }
                cmd.Dispose();
                conn.Close();
                //将二进制数据buffer显示为图片
                MemoryStream ms = new MemoryStream(buffer);//构建对象
                ms.Write(buffer,0,buffer.Length);//写到内存流中
                Image img = System.Drawing.Image.FromStream(ms);
                pictureBox1.Image = img;
    
            }
        }
    }
  • 相关阅读:
    批量修改文件的名字
    字节码指令以及操作数栈的分析
    字节码文件的分析
    类加载器详解
    类的加载-连接-初始化
    电商订单ElasticSearch同步解决方案--使用logstash
    springboot整合Mangodb实现crud,高级查询,分页,排序,简单聚合
    mongodb安装教程(亲测有效)
    Azure : 通过 SendGrid 发送邮件
    用java实现删除目录
  • 原文地址:https://www.cnblogs.com/dlexia/p/4648992.html
Copyright © 2020-2023  润新知