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(以 ...
随机推荐
- [置顶] PHP开发实战权威指南-读书总结
从今年开始,断断续续学习PHP已经有4个月了. 最初,认真学习PHP几天,就弄WordPress搭建了一个个人博客,这也符合技术人的实践理念. 最近,重温PHP开发实战权威指南,做点总结,整理下自己学 ...
- 最全的LBS手机定位技术说明
随着手机技术的发展定位方式也发生了非常大的变化.获取手机位置有非常多种方式. 第一种:CELL-ID定位原理 通过移动网络获取设备当前所在的Cell信息来获取设备当前位置.当设备位置更新设备会向当前服 ...
- 权限管理之基于ACL的实现:自定义JSTL函数实现即时认证
实现即时认证(即只有拥有相应的权限,才能做相应的操作) 经常用在,在JSP页面上,调用JSTL自定义函数做判断,显示相应的菜单或者功能按钮,比如只有管理员登陆时才显示“删除”按钮,从而完成权限的即时认 ...
- EntityFramework经典的left join语法
/* * 常常看到有人问linq语法怎样写left join的查询语句,但网上找到的都是简单的两表连接.參考意义有限. * 今天最终项目里要用到复杂的多表连接,同一时候含有多个左连接, * 恰好 ...
- vhost文件设置
#例子<VirtualHost *.82> #设置端口号为82 ServerName localhost #服务器名称 DocumentRoot "d:/Web" #文 ...
- Clojure学习02:语法
相比我们传统的 c ,java ,python ,javascript等,Clojure的语法比较特别,初一看,还可能会有些不适应. 本文来介绍下Clojure的语法特点. 一.表达式 所有的Cloj ...
- Android跟蓝牙耳机建立连接有两种方式
Android 跟蓝牙耳机建立连接有两种方式. 1. Android 主动跟蓝牙耳机连BluetoothSettings 中和蓝牙耳机配对上之后, BluetoothHeadsetService 会收 ...
- BZOJ 2795: [Poi2012]A Horrible Poem( hash )
...字符串hash. 假如长度x是一个循环节, 那么对于任意n(x | n)也是一个循环节. 设当前询问区间[l, r]长度为len = ∏piai, 最终答案ans = ∏piai' ,我们只需枚 ...
- Eclipse使用技巧总结(二)
七.快速切换打开的文件 Ctrl + F6 八.快速大写.小写转换 Ctrl + Shift + Y Ctrl + Shift + X 九.快速删除光标所在行 Ctrl + D 十.快速复制 Ctrl ...
- [转]Centos 6.5 优化 一些基础优化和安全设置
关于CentOS服务器的优化下文作为参考. 本文 centos 6.5 优化 的项有18处: 1.centos6.5最小化安装后启动网卡2.ifconfig查询IP进行SSH链接3.更新系统源并且升级 ...