C# - 数据库存取图片
2015-06-22 11:31
591 查看
1、创建数据表
运行效果:
2、代码:
参考链接:http://www.sufeinet.com/thread-1261-1-1.html
、
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
、
相关文章推荐
- grails数据库逆向生成
- [转]一个简单的生成随机手机/电话号码/身份证号/银行卡号/地址/电子邮件等的Oracle小程序
- .sql文件的使用
- oracle 日志删除的方法,转于别人的文章且验证有效
- Oracle之约束
- Oracle之约束
- 《mysql 必知必会》 笔记(四)
- SQLServer 打开时的连接问题
- Oracle之约束总结
- Oracle查询大于1000条处理
- mysql 按周、日进行数据统计
- 向mysql数据库存换行
- oracle 和mysql触发器的编写有什么不同。
- oracle性能调优学习0622
- sql多表查询实例
- MySQL优化之——索引
- mysql免安装版配置手册
- Redis 连接池的问题
- Import SQL into MySQL with a progress meter
- CentOS 安装Oracle 11g R2