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

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

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

 

示例代码:

保存图片到MySQL

 

private void btnOpenFile_Click(object sender, EventArgs e)  

  • 1.
  • 2.

打开图片文件

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

连接字符串

 

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

 

 

从MySQL中读取并显示图片

 

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

Oracle 存储过程

2010-06-01 14:17:44

MySQL重启命令

2010-06-04 14:18:10

MySQL 分页存储过

2010-04-26 00:42:08

DNS负载均衡

2010-05-18 17:39:13

MySQL alter

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技术栈公众号