陈斌彬的技术博客

Stay foolish,stay hungry

C# 保存图片到 SQL

新建 Windows 窗体应用程序

img

选择工具箱

img

拖动两个Button,一个PictureBox,一个openFileDialog到Form中

img

右键项目选择“查看代码”,进行代码编辑

img

按钮一保存图片

img

按钮二显示图片

img

SQL中数据格式显示

数据类型选择 image

img

img

启动调试,显示图片

img

代码实现

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //保存图片到SQL
        private void button1_Click(object sender, EventArgs e)
        {
            string fName;
            OpenFileDialog openFileDialog = new OpenFileDialog();//实例化
            openFileDialog.InitialDirectory = @"c:\";//打开的默认路径
            openFileDialog.Filter = "图像文件 (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"; 
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                fName = openFileDialog.FileName;
                FileStream fs = new FileStream(fName, FileMode.Open);
                byte[] imgbt = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                imgbt = br.ReadBytes(Convert.ToInt32(fs.Length));

                string cnnstr = "Data Source=JOHN;Initial Catalog=webservice;User ID=sa;Password=12345678";
                SqlConnection conn = new SqlConnection(cnnstr);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;

                string sql = "insert into imagetable values(@image)";
                comm.CommandType = CommandType.Text;
                comm.CommandText = sql;
                comm.Parameters.Add("image", SqlDbType.Image, imgbt.Length);
                comm.Parameters[0].Value = imgbt;
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }

        //读取图片,进行显示
        private void button2_Click(object sender, EventArgs e)
        {
                string cnnstr = "Data Source=JOHN;Initial Catalog=webservice;User ID=sa;Password=12345678";

                string sql = "select * from  imagetable";
                SqlConnection conn = new SqlConnection(cnnstr);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandType = CommandType.Text;
                comm.CommandText = sql;
                SqlDataReader dr = comm.ExecuteReader();

               while (dr.Read())
               {
                   if (dr["imageLabel"] != DBNull.Value)
                   {
                       MemoryStream ms = new MemoryStream((byte[])dr["imageLabel"]);//把照片读到MemoryStream里      
                       Image imageBlob = Image.FromStream(ms, true);//用流创建Image  

                       pictureBox1.Image = imageBlob;//输出图片      
                   }
                   else//照片字段里没值,清空pb      
                   {
                       pictureBox1.Image = null;
                   }
               }
        }
    }
}