存取数据库图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
 
namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
           // WebRequest wr = HttpWebRequest.Create("http://www.itnba.com");
 
            
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "jpg图|*.jpg|png图|*.png|gif图|*.gif|所有文件|*.*";
            DialogResult isok = openFileDialog1.ShowDialog();
 
            if (isok == DialogResult.OK)
            {
                //开始使用流读取
                FileStream fs = new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
                //使用流读取器,把文件流对象中的内容读取出来,转换成字符串或者其他对应的数据
                BinaryReader br = new BinaryReader(fs);//二进制读取器
                byte[] imgbytes = br.ReadBytes((int)fs.Length);//将流中数据读取成byte数组存入数组变量中
 
                //连接数据库,新增数据
                SqlConnection conn = new SqlConnection("server=.;database=aaa;user=sa;pwd=123");
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "insert into imgtable values(@img)";<br>                //数据库操作语句
                cmd.Parameters.Clear();<br>
                cmd.Parameters.Add("@img",imgbytes);
                conn.Open();
 
                cmd.ExecuteNonQuery();
 
                conn.Close();
 
 
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            //连接数据库,新增数据
            SqlConnection conn = new SqlConnection("server=.;database=aaa;user=sa;pwd=123");
 
            SqlCommand cmd = conn.CreateCommand();
 
            cmd.CommandText = "select top 1 *from imgtable order by ids desc";
 
            conn.Open();
             //ExecuteReader 数据库读取用的
            SqlDataReader dr = cmd.ExecuteReader();
            byte[] imgbytes = null;<br>          //定义一个数组 
            if (dr.Read())
            {
               imgbytes  = (byte[])dr["images"];
            }
 
            conn.Close();
            //如何把二进制数据,转换成一个Image类型,来给piceturebox赋值
            //内存流
            MemoryStream ms = new MemoryStream(imgbytes);
            Image img = Image.FromStream(ms);
 
            pictureBox2.Image = img;
 
 
           // Image img = Image.
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "jpg图|*.jpg|png图|*.png|gif图|*.gif|所有文件|*.*";
            DialogResult isok = openFileDialog1.ShowDialog();
 
            if (isok == DialogResult.OK)
            {
                Image img = Image.FromFile(openFileDialog1.FileName);
                pictureBox1.Image = img;
            }
        }
    }
}<br>
原文地址:https://www.cnblogs.com/zxm1002/p/4939732.html