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(以 ...
随机推荐
- Sublime Text 高级正则查换替换功能
有一个需求:把某从mysql 里导出的的数据表 数据里的 Insert语句 转换成 update 语句. 须要把例如以下的语句: insert into `table` (`ID`, `code`, ...
- 横瓜先生如何用MDB和XLS等低性能数据库来处理千亿级数据量。
横瓜先生如何用MDB和XLS等低性能数据库来处理千亿级数据量. 横瓜先生曾经用ACCESS做数据库,开发出高性能CMS来处理过TB级的文本数据量,任何请求都可以在10MS内完成,基本就是硬盘延迟的时间 ...
- 一个IP每天只弹一次广告窗口
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- stm32之Systick(系统时钟)
Systick的两大作用: 1.可以产生精确延时: 2.可以提供给操作系统一个单独的心跳(时钟)节拍: 通常实现Delay(N)函数的方法为: for(i=0;i<x;i++) ; 对于STM3 ...
- CodeForces 486C Palindrome Transformation 贪心+抽象问题本质
题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...
- 【QT相关】Image Viewer Example
结合QLable和QScrollArea显示一个图片. QLable典型用于用户展示文本,但是也能展示图片.QScrollArea提供了一个滚动视图,如果子控件超过了框架限制,QScrollArea自 ...
- 缩略图类库--ThumbLib使用简介
//加载类库文件 require_once 'path/to/ThumbLib.inc.php'; //实例化类库,传入你要处理的图片的地址可以是网络地址,也可以是本地地址 $thumb = PhpT ...
- 解决Spring中singleton的Bean依赖于prototype的Bean的问题
在spring bean的配置的时候,可能会出现一个singleton的bean依赖一个prototype的bean.因为singleton的bean只有一次初始化的机会,所以他们的依赖关系页只有在初 ...
- 17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制对于新的Master和Slaves:
17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制对于新的Master和Slaves: 最简单和最直接的方法是设置复制用于使 ...
- CCIE路由实验(4) -- BGP路由控制
1.过滤BGP路由的方法2.用AS-path filter控制路由3.用Community Filter控制路由 enableconf tno ip do loenable pass ciscolin ...