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# - 数据库存取图片的更多相关文章

  1. android开发之数据库存取图片

    Android数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型).对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢 ...

  2. android 数据库存取图片

    Android数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型).对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢 ...

  3. delphi使用ADO在sql数据库存取图片的方法

    我一直不认为能把代码写的和天书一样的程序员是好的程序员,那不过是因为我真的对delphi也就是略懂皮毛,太深了看不懂.网上查询数据库存取图片的方式,看的是一头雾水,有人提出保存路径使用时再调用,方法很 ...

  4. 复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等

    使用配置文件properties进行连接数据库 首先创建一个file自定义文件名,但是后缀名必须改为.properties(不分大小写):如config.properties: 然后双击config. ...

  5. 小谈c#数据库存取图片的方式

    第一种方式   文件夹与数据库配合 /// <summary> /// 上传图片 /// </summary> /// <param name="FUSShop ...

  6. redis 几种数据类型往数据库存数据和取数据的帮助类

    package com.fndsoft.bcis.utils; import org.springframework.beans.factory.annotation.Autowired; impor ...

  7. mysql数据库存中文字段

    mysql数据默认编码是拉丁,而我们更多的使用utf8, 在创建库的时候执行参数即可: CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET ...

  8. 我的头上碧空晴朗——数据库存datetime问题

    今天遇到一个问题,数据库mysql存的datetime类型数据.取出来数据居然耍流氓,好好的日期在秒后多了个小数点0 当我用正常的方法, SimpleDateFormat myFmt=new Simp ...

  9. linux下安装mongo数据库存

    https://www.runoob.com/mongodb/mongodb-linux-install.html 安装完后,要重启一下,否则无法运行./mongod 下载完安装包,并解压 tgz(以 ...

随机推荐

  1. JAVA GUI学习 - 窗体背景图片设置方法:重写paintComponent(Graphics g)方法

    public class BackgroundImage extends JFrame { public BackgroundImage() { this.setTitle("窗体背景图片设 ...

  2. Uva 3226 Symmetry

    题目给出一些点的坐标(横坐标,纵坐标),没有重叠的点,求是否存在一条竖线(平行于y轴的线),使线两边的点左右对称. 我的思路:对于相同的纵坐标的点,即y值相同的点,可以将x的总和计算出,然后除以点的数 ...

  3. pragma comment

    pragma指令简介 在编写程序的时候,我们经常要用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. 下面介绍了一下该指令的一些常用参数,希望对大家有所帮助! 一. mess ...

  4. node.js第十课(HTTPserver)

     概念:Node.js提供了http模块.当中封装了一个高效的HTTPserver和一个简单的HTTPclient.     http.server是一个基于事件的HTTP服务器.内部用C++实现 ...

  5. C#_会员管理系统:开发一(用户登录)

    首先创建数据库: [Vip] 创建三张表: 分别是: [VipInformation](会员信息) [Log](日志) [VipAccount](账户权限) 详细语句: --创建数据库[Vip] cr ...

  6. 64位系统/32位系统下/8位CPU的数据宽度

    不同的编译器根据不同的 64 位模型有所不同. 比如 Visual C++,从第一个支持 64 位的版本起,一直就是使用 LLP64 内存模型,也就是说,编译出的代码除了 long 和指针是 64 位 ...

  7. ubuntu安装ulipad

    以下内容部分我是从其他地方找的,并且做了适当的修改,亲身测试可以安装成功   在安装ulipad之前,先安装一个超级好用的Python的交互式Shell--iPython.iPython功能很强大, ...

  8. SGU 183. Painting the balls( dp )

    dp..dp(i, j)表示画两个点为i-j, i的最优答案. dp(i, j) = min{ dp(i-j, k) } + cost[i] (1≤k≤M-j) 令f(i, j) = min{dp(i ...

  9. int_float_double数据类型的存储格式。

    一段用来检测编辑器存储方式的程序 //date : 2013/8/16 //designer :pengxiaoen //function check the C programmable langu ...

  10. eclipse 找不到application选项

    处理如下:Window-Preferences-Run/Debug-Perspectives 中的 And Build修改为如下