加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql数据库怎样储存读取图片

发布时间:2022-02-22 16:33:47 所属栏目:MySql教程 来源:互联网
导读:这篇文章主要介绍了mysql数据库怎样储存读取图片,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。 mysql储存读取图片的方法:首先将图片转换成缓冲流;然后获得图片的字节数组并执行相关操作
       这篇文章主要介绍了mysql数据库怎样储存读取图片,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。
 
      mysql储存读取图片的方法:首先将图片转换成缓冲流;然后获得图片的字节数组并执行相关操作;最后通过“public void MapSearchQuery(out byte[] imageByteResulet){...}”读取图片即可。
 
      首先,介绍一下mysql相关的数据类型:MySQL中有四种BLOB类型,TinyBlob(最大255Byte), Blob(最大65K), MediunBlob(16M), LongBlob(最大4G)。这里注意一下如果你数据库出现相关的Data too long...字样可能是你选择的种类的大小不够。
 
      接下来简单说一下我为什么没有用存储图片路径的方式,而采取了直接在MySQL中存储图片的方式。原因有两点:
 
   1、本身不需要大量图片,一个数据库只需要一张图片
 
   2、软件结构是要通过WebService由一个主客户端去访问下面附属的几个客户端,如果附属客户端不存储图片直接供主客户端访问,那么主客户端势必就需要一个加载图片的功能(类似于FTP的功能)。
 
   下面还是直接上代码吧:
 
public bool MapSearchWrite(string strImagePath)
         {
             //将图片转换成缓冲流
             FileStream fs = new FileStream(strImagePath, FileMode.Open, FileAccess.Read);
             
             //获得图片的字节数组
             byte[] byImage = new byte[fs.Length];
             fs.Read(byImage, 0, byImage.Length);
             fs.Close();
             //数据库连接
             MySqlConnection conn = new MySqlConnection();
             conn.ConnectionString = "Server=localhost;Uid=root;Password=123456;Database=firefighting;charset=gb2312";
             try
             {
                 conn.Open();
             }
             catch
             {
                 conn.Close();
                 conn.Dispose();
                 throw new ArgumentException("地图检索数据库连接失败");
             }
             //判断数据库内部有无记录
             string strQueryCmd = "select PicNum from images";
             MySqlCommand cmdQuery = new MySqlCommand(strQueryCmd, conn);
             MySqlDataReader dataReader = cmdQuery.ExecuteReader();
             //执行操作
             MySqlCommand cmd = new MySqlCommand();
             if (dataReader.Read())
             {
                 cmd.CommandText = "update images set Image=@byImage";
             }
             else
             {
                 cmd.CommandText = "insert into images(Image) values(@byImage)";
             }
           
             cmd.CommandType = CommandType.Text;
             cmd.Parameters.Add("@byImage", MySqlDbType.MediumBlob);
             cmd.Parameters[0].Value = byImage;
             cmd.Connection = conn;
          
             int affectedRows = 0;
             try
             {
                 affectedRows = cmd.ExecuteNonQuery();
             }
             catch
             {
                 affectedRows = -1;
             }
             //关闭连接等
             cmd.Dispose();
             conn.Close();
             conn.Dispose();
             if (affectedRows <= 0)
             {
                 return false;
             }
             else
             {
                 return true;
             }
         }
   这是把图片插入到数据库的操作代码,路径的话就是你所需要存储的图片所在的路径(包括图片的名字和后缀名哦),另外我这里采用的是ADO.NET的连接方式,语言是C#的,其他代码也不用我解释了......
 
   下面是读取MySQL中的图片的代码
 
 public void MapSearchQuery(out byte[] imageByteResulet)
         {
             imageByteResulet = null;
             MySqlConnection conn = new MySqlConnection();
             conn.ConnectionString = "Server=localhost;Uid=root;Password=123456;Database=firefighting;charset=gb2312";
             try
             {
                 conn.Open();
             }
             catch
             {
                 conn.Close();
                 conn.Dispose();
                 throw new ArgumentException("地图检索数据库连接失败");
             }
             string strQueryCmd = "select Image from images limit 1";
             MySqlCommand cmd = new MySqlCommand(strQueryCmd, conn);
             MySqlDataReader dataReader = null;
             try
             {
                 dataReader = cmd.ExecuteReader();
             }
             catch
             {
                 dataReader.Dispose();
                 cmd.Dispose();
                 conn.Close();
                 conn.Dispose();
                 throw new ArgumentException("地图检索查询地图失败");
             }
             if (dataReader.Read())
             {
                 imageByteResulet = new byte[dataReader.GetBytes(0, 0, null, 0, int.MaxValue)];
                 dataReader.GetBytes(0, 0, imageByteResulet, 0, imageByteResulet.Length);
                 //将图片字节数组加载入到缓冲流
                 // MemoryStream imageStream = new MemoryStream(imageByte);
                 //从缓冲流生成图片
                 //imageResulet = Image.FromStream(imageStream, true);
             }
             dataReader.Dispose();
             cmd.Dispose();
             conn.Close();
             conn.Dispose();
         }
   当然这里我是照顾到Image对象不能通过WebService传输而把BLOB数据只转换成byte[]在传输,如果不需要这个功能的换可以直接把相关代码踢出来再将byte[]转成图片对象即可,一下提供两种方法
 
第一种:imageByte是调用上面函数得到的byte[]的数据
 
//将图片字节数组加载入到缓冲流  
MemoryStream imageStream = new MemoryStream(imageByte);
                //从缓冲流生成图片                
                imageResulet = Image.FromStream(imageStream, true);
                //pictureBox是一个显示图片或者视频的C#控件
                pictureBox.Image = imageResulet;
这样就把图片读取出来并显示出来了
 
第二种:BitMap是System.Drawingm命名空间中的
 
Bitmap bm = new Bitmap(new MemoryStream(
imageByte
));
            
    
 pictureBox1.Image = bm;
那么,到此我就说完了,当然不是迫不得已不要把图片存到数据库中,可以做个url映射,返回文件流。
 
感谢你能够认真阅读完这篇文章,希望小编分享mysql数据库怎样储存读取图片内容对大家有帮助。

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!