码迷,mamicode.com
首页 > 其他好文 > 详细

读取二进制大对象

时间:2017-12-12 10:28:49      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:win   where   str   exe   model   pen   ali   dial   void   

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

 

读取二进制大对象

标签:win   where   str   exe   model   pen   ali   dial   void   

原文地址:http://www.cnblogs.com/zhang1997/p/8026473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!