C# - 数据库存取图片

时间:2024-04-16 11:37:28

1、创建数据表

CREATE TABLE Tb_pic
(
ID int primary key identity(1, 1) not null,
PictureBox varchar(max)
)

运行效果:

C# - 数据库存取图片

2、代码:

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.Data.SqlClient;
using System.Configuration;
using System.IO; namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //定义字节数组,用来存储图片
byte[] arr; //抽取表中的ID字段,绑定combBox数据
public void GetID()
{
this.comboBox1.Items.Clear();
string constring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string sql = "select ID from Tb_pic";
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
this.comboBox1.Items.Add(sdr["ID"].ToString());
}
this.comboBox1.SelectedIndex = 0;
} /// <summary>
/// 加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
GetID();
} /// <summary>
/// 浏览事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Title = "请选择客户端longin的图片";
openfile.Filter = "Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if (DialogResult.OK == openfile.ShowDialog())
{
try
{
Bitmap bmp = new Bitmap(openfile.FileName);
pictureBox1.Image = bmp;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
}
catch { }
}
} /// <summary>
/// 保存事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (arr == null)
{
MessageBox.Show("照片为空!","提示");
return;
} //直接返这个值放到数据就行了 string sql = "insert into Tb_pic (PictureBox) values (@pic)"; SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@pic", Convert.ToBase64String(arr))
}; string constring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(para);
int i = cmd.ExecuteNonQuery();
con.Close(); if (i == 1)
{
MessageBox.Show("添加成功!", "提示");
GetID();
}
else
{
MessageBox.Show("添加失败!", "提示");
}
} /// <summary>
/// 显示事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (this.comboBox1.Items.Count <= 0)
{
MessageBox.Show("无数据!", "提示");
return;
} try
{
string sql = "select PictureBox from Tb_pic where ID = '" + this.comboBox1.SelectedItem.ToString() + "'";
string constring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con); string pic = (string)cmd.ExecuteScalar(); // pic=........这一句换成从数据库里读取就可以了
//判断是否为空,为空时的不执行
if (!string.IsNullOrEmpty(pic))
{
//直接返Base64码转成数组
byte[] imageBytes = Convert.FromBase64String(pic);
//读入MemoryStream对象
MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
//转成图片
Image image = Image.FromStream(memoryStream); //memoryStream.Close();//不要加上这一句否则就不对了 // 将图片放置在 PictureBox 中
this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox2.Image = image;
}
}
catch { }
} /// <summary>
/// 取消选中图片事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
arr = null;
this.pictureBox1.Image = null;
}
}
}

参考链接:http://www.sufeinet.com/thread-1261-1-1.html