C# - 数据库存取图片
1、创建数据表
CREATE TABLE Tb_pic
(
ID int primary key identity(1, 1) not null,
PictureBox varchar(max)
)
运行效果:

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
、
C# - 数据库存取图片的更多相关文章
- android开发之数据库存取图片
Android数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型).对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢 ...
- android 数据库存取图片
Android数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型).对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢 ...
- delphi使用ADO在sql数据库存取图片的方法
我一直不认为能把代码写的和天书一样的程序员是好的程序员,那不过是因为我真的对delphi也就是略懂皮毛,太深了看不懂.网上查询数据库存取图片的方式,看的是一头雾水,有人提出保存路径使用时再调用,方法很 ...
- 复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等
使用配置文件properties进行连接数据库 首先创建一个file自定义文件名,但是后缀名必须改为.properties(不分大小写):如config.properties: 然后双击config. ...
- 小谈c#数据库存取图片的方式
第一种方式 文件夹与数据库配合 /// <summary> /// 上传图片 /// </summary> /// <param name="FUSShop ...
- redis 几种数据类型往数据库存数据和取数据的帮助类
package com.fndsoft.bcis.utils; import org.springframework.beans.factory.annotation.Autowired; impor ...
- mysql数据库存中文字段
mysql数据默认编码是拉丁,而我们更多的使用utf8, 在创建库的时候执行参数即可: CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET ...
- 我的头上碧空晴朗——数据库存datetime问题
今天遇到一个问题,数据库mysql存的datetime类型数据.取出来数据居然耍流氓,好好的日期在秒后多了个小数点0 当我用正常的方法, SimpleDateFormat myFmt=new Simp ...
- linux下安装mongo数据库存
https://www.runoob.com/mongodb/mongodb-linux-install.html 安装完后,要重启一下,否则无法运行./mongod 下载完安装包,并解压 tgz(以 ...
随机推荐
- [python]通过urllib2设置代理访问网址
#!/usr/bin/env pythonimport urllib2 # change followings before useuser = 'foo'passwd = 'bar'proxyser ...
- Python 連接 MySQL
Python 連接 MySQL MySQL 是十分流行的開源資料庫系統,很多網站也是使用 MySQL 作為後台資料儲存,而 Python 要連接 MySQL 可以使用 MySQL 模組.MySQLdb ...
- 颜色空间RGB与HSV(HSL)的转换
一般的3D编程只需要使用RGB颜色空间就好了,但其实美术人员更多的是使用HSV(HSL),因为可以方便的调整饱和度和亮度. 有时候美术需要程序帮助调整饱和度来达到特定风格的渲染效果,这时候就需要转换颜 ...
- js实现无限级树形导航列表
- 开源的Delphi性能调试工具
官网:http://dbg-spider.net/源码:https://github.com/yavfast/dbg-spider Real time profiler for Delphi appl ...
- JProtector 帮助文档
一.应用加密 1.使用 JProtector在线 进行应用加密:使用浏览器访问 http://app.shuton.net/encryptjar, 点击 Browse 选择待加密的应用jar包.war ...
- Event | Beijing Makerspace
Event | Beijing Makerspace CONTACT INFORMATION 4th Floor, Zhongguancun Dream Lab, Beijing, China Pho ...
- win8安装驱动提示文件哈希值不在指定的目录文件中,此文件可能已损坏或被篡改解决办法
解决办法: 1. 按快捷键win+R 打开运行命令 2. (请先看完后面的再操作!!)运行输入 shutdown.exe /r /o /f /t 00 3. 点击确定 4. 系统将重启 5. 重启后点 ...
- mongodb进阶一之高级查询
上篇文章我们讲了mongodb的crud基本操作 http://blog.csdn.net/stronglyh/article/details/46812579 这篇我们来说说mongodb的进阶-- ...
- NYOJ-开灯问题
开灯问题 时间限制:3000 ms | 内存限制:65535 KB 难度: 描写叙述 有n盏灯,编号为1~n.第1个人把全部灯打开,第2个人按下全部编号为2 的倍数的开关(这些灯将被关掉),第3 ...