zoukankan      html  css  js  c++  java
  • 数据库存储图片和读取图片

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog op = new OpenFileDialog();
                DialogResult isok = op.ShowDialog();
                if (isok==DialogResult.OK)
                {
                    //公开以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。
                    FileStream fs = new FileStream(op.FileName, FileMode.Open);//mode一个常数,用于确定如何打开或创建文件。
                    Image img = System.Drawing.Bitmap.FromStream(fs);
                    pictureBox1.Image = img;
                    fs.Close();
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog op = new OpenFileDialog();
                DialogResult isok = op.ShowDialog();
                if (isok == DialogResult.OK)
                {
                    //读取图片存入数据库
                    FileStream fs = new FileStream(op.FileName, FileMode.Open);
                    BinaryReader br = new BinaryReader(fs);//二进制读取器
                    byte[] buffer=new byte[fs.Length];//二进制字节数组
                    buffer=br.ReadBytes((int)fs.Length);
                    SqlConnection conn = new SqlConnection("server=.;database=car;uid=sa;pwd=123");
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into pic values(4,@buffer)";
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@buffer",buffer);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    cmd.CommandText = "select top 1 * from pic order by code desc";
                    SqlDataReader dr = cmd.ExecuteReader();
                    byte[] by = new byte[fs.Length];
                    if (dr.Read())
                    {                                       
                        by=(byte[])(dr["pics"]);
                    }
                    MemoryStream ms = new MemoryStream(by);
                    Image img = System.Drawing.Bitmap.FromStream(ms);
                    pictureBox1.Image = img;
                }
            }
        }
    }
  • 相关阅读:
    新收入准则下的通用收入处理场景
    如果不先提出最佳问题,你怎么去找最佳答案呢?
    复星集团
    CFO的三重境界:阿里CFO蔡崇信教给我的那些事儿
    《让财务助推业务—业财融合》
    一、原材料、半成品、成品的采用标准成本法管理 [转发]
    顶级投行?IT->Data Analysis->Forecast->Future.
    SAP 合资公司解决方案
    SAP-关于分类账(Ledgers)的总结
    百度网盘目录树在线V2.0版功能介绍~
  • 原文地址:https://www.cnblogs.com/happinesshappy/p/4562890.html
Copyright © 2011-2022 走看看