MySQL保存jpg 图片的实际操作过程

数据库 MySQL
我们今天主要向大家描述的是MySQL保存jpg 图片的实际操作流程,以及对实现MySQL保存jpg 图片的实际应用代码的介绍。

以下的文章主要介绍的是MySQL保存jpg 图片的实际操作过程,我们大家都知道MySQL数据库下可以通过blob, mediumbolb,l ongblob等一些类型来保存图片,当然不同的相关图片文件类似操作会有所不同,例如.bmp格式图片。

 

示例代码:

保存图片到MySQL

 

  1. private void btnOpenFile_Click(object sender, EventArgs e)  

打开图片文件

  1. this.openFileDialog1.InitialDirectory = "C:\\";  
  2. this.openFileDialog1.FileName = "";  
  3. this.openFileDialog1.ShowDialog(); 

连接字符串

 

  1. string connStr = "server=vitus;User Id=root;Password=******;Persist Security Info=True;database=Test";  
  2. string sql = string.Format("insert into ImageTest values(@id,@picture)");  
  3. FileStream fs = new FileStream(this.openFileDialog1.FileName,FileMode.Open);  
  4. Byte[] bts = new Byte[fs.Length-1];  
  5. fs.Read(bts,0,(int)fs.Length-1);  
  6. MySqlConnection sqlConn = new MySqlConnection(connStr);  
  7. MySqlCommand sqlComm = new MySqlCommand(sql,sqlConn);  
  8. sqlComm.Parameters.Add("@id", MySqlDbType.Int32, 1);  
  9. sqlComm.Parameters["@id"].Value = 2;  
  10. sqlComm.Parameters.AddWithValue("@picture", bts);  
  11. sqlConn.Open();  
  12. sqlComm.ExecuteNonQuery();  
  13. sqlConn.Clone();  
  14. }  

 

 

从MySQL中读取并显示图片

 

  1. private void btnImageView_Click(object sender, EventArgs e)  
  2. {  
  3. string connStr = "server=vitus;User Id=root;Password=******;Persist Security Info=True;database=Test";  
  4. string sql = string.Format("select * from ImageTest where id=2");  
  5. MySqlConnection sqlConn = new MySqlConnection(connStr);  
  6. MySqlCommand sqlComm = new MySqlCommand(sql, sqlConn);  
  7. sqlConn.Open();  
  8. MySqlDataReader dr = sqlComm.ExecuteReader(CommandBehavior.CloseConnection);  
  9. Image image = null;  
  10. while (dr.Read())  
  11. {  
  12. MemoryStream buff = new MemoryStream((byte[])dr[1]);  
  13. image = Image.FromStream(buff, true);  
  14. buff.Close();  
  15. }  
  16. this.pictureBox1.Image = image;  
  17. }  

 

 

以上的相关内容就是对MySQL保存jpg图片的介绍,望你能有所收获。

【编辑推荐】

  1. MySQL 游标的具体使用方案
  2. MySQL show的实际操作用法
  3. MySQL数据库中的5种数据类型简介
  4. MySQL导入导出.sql文件实践演练
  5. MySQL root密码忘记的解决

 

责任编辑:佚名 来源: 互联网
相关推荐

2010-05-17 13:28:15

MySQL 复制

2010-03-30 12:50:42

Oracle存储

2010-06-30 12:39:11

2010-03-16 15:16:01

Python web框

2010-08-05 09:33:31

DB2数据库卸载

2011-02-24 14:23:18

2010-03-22 18:53:53

Python格式化字符

2009-08-25 15:48:03

C#数组操作

2010-05-26 14:55:43

MySQL存储过程

2010-01-06 11:30:22

.NET Framew

2009-12-11 17:29:22

Linux桌面

2010-06-01 14:17:44

MySQL重启命令

2010-04-07 13:02:14

Oracle 存储过程

2010-06-04 14:18:10

MySQL 分页存储过

2010-05-18 17:39:13

MySQL alter

2010-04-26 00:42:08

DNS负载均衡

2010-04-14 13:18:53

安装无线适配器

2009-12-16 17:11:10

Fedora 挂载

2010-05-19 11:25:46

MySQL触发器

2010-06-12 13:39:33

MySQL操作blob
点赞
收藏

51CTO技术栈公众号