zoukankan      html  css  js  c++  java
  • 读取二进制大对象

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    
    namespace main
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnUpload_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "*.jpg|*.jpg|*.png|*.png|*.bmp|*.bmp";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string fileName = ofd.FileName;
                    FileStream fs = new FileStream(fileName, FileMode.Open);
                    byte[] imageBytes = new byte[fs.Length];
                    BinaryReader br = new BinaryReader(fs);
                    imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
    
                    string s = "server=PC-20171113RBMO;database=StudentDB;Trusted_Connection=true";
                    SqlConnection con = new SqlConnection(s);
    
                    string c = "insert into Pic(image) values(@pic)";
                    SqlCommand cmd = new SqlCommand(c, con);
                    SqlParameter para = new SqlParameter("@pic", SqlDbType.Image);
                    para.Value = imageBytes;
                    cmd.Parameters.Add(para);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
    
            private void btnRead_Click(object sender, EventArgs e)
            {
                string s = "server=PC-20171113RBMO;database=StudentDB;Trusted_Connection=true";
                SqlConnection con = new SqlConnection(s);
                string c = "select image from Pic where ID = " + textBox1.Text.Trim();
                SqlCommand cmd = new SqlCommand(c, con);
                con.Open();
                byte[] image = (byte[])cmd.ExecuteScalar();
                con.Close();
    
                MemoryStream ms = new MemoryStream(image);
                Bitmap bmp = new Bitmap(ms);
                pictureBox1.Image = bmp;
            }
        }
    }
  • 相关阅读:
    每日总结
    体温登记app(大年初一要收的作业)慢慢更,这个写完了
    2021/01/31周学习总结
    2021/01/24周学习总结
    从小工到专家
    构建之法阅读笔记
    2021/01/17周学习总结
    人月神话阅读笔记
    利用Word制作Kindle用的6寸PDF电纸书
    面试题-谈谈封装和抽象的区别(转)
  • 原文地址:https://www.cnblogs.com/zhang1997/p/8026473.html
Copyright © 2011-2022 走看看