c# – 从MySQL中检索中等blob只返回13个字节

c# – 从MySQL中检索中等blob只返回13个字节,第1张

概述我搜索了很多并尝试了各种方法,但无法解决这个问题. 我需要将图像保存到 mysql文件中. 我使用下面的代码将图像保存到数据库中. try{ string location = @"C:\Users\test\Downloads\Photos\batman-greenscreen.jpg"; FileStream fs = new FileStream(location, Fi 我搜索了很多并尝试了各种方法,但无法解决这个问题.
我需要将图像保存到 mysql文件中.

我使用下面的代码将图像保存到数据库中.

try{    string location = @"C:\Users\test\Downloads\Photos\batman-greenscreen.jpg";    fileStream fs = new fileStream(location,fileMode.Open,fileAccess.Read);    UInt32 fileLength = (UInt32)fs.Length;    byte[] buffer = new byte[fileLength];    fs.Read(buffer,(int)fileLength);    string sqlPhotoquery = "INSERT INTO tab_photo VALUES('" + photo.PhotoID + "','" + photo.ProjectID + "','" + photo.Day + "','" + photo.barcode + "','" + photo.Photoname + "','" + photo.PhotoXml + "','" + buffer + "','" + fileLength + "')";    int result = MysqLHelper.ExecuteNonquery(connectionString,sqlPhotoquery);    if (result > 0)        return true;    else        return false;}catch (Exception e){    return false;}

在此文件中图像长度为12428,

我试图从数据库中检索数据:

Photo photo = new Photo();try{    string sqlquery = "SELECT * FROM tab_photo where PhotoID='"+photoID+"'";    MysqLDataReader rdr = MysqLHelper.ExecuteReader(connectionString,sqlquery);    while (rdr.Read())    {        photo.PhotoID = rdr.GetString("PhotoID");        photo.ProjectID = rdr.GetString("ProjectID");        photo.Day = rdr.GetInt32("Day");        photo.barcode = rdr.GetString("barcode");        photo.Photoname = rdr.GetString("Photoname");        photo.PhotoXml = rdr.GetString("PhotoXml");        MemoryStream ms;        UInt32 fileSize;        Bitmap outimage;        int fileSize = rdr.GetInt32(rdr.Getordinal("PhotoSize"));        byte[] rawData = new byte[fileSize];        rdr.GetBytes(rdr.Getordinal("Photo"),rawData,(Int32)fileSize);        photo.imageByte = rawData;    }}catch (Exception e){}

但是当我检索文件时,它只检索前13个字节.之后,它的值为零,并且检索值存在差异.

我遵循了Handling BLOB Data With Connector/Net中提到的说明.

根据我已经改变我的设置如下:

这是数据保存在数据库中的方式

我无法获得正确值的原因是什么?在数据保存或数据检索或数据库配置方面有任何问题吗?

解决方法 您无法在数据库中拥有正确的数据.通过将缓冲区连接到查询字符串中,“System.Byte []”将被插入到数据库中,因为insert语句基本上看起来像

INSERT INTO tab_photo VALUES(...,'System.Byte[]',...)";

像在示例中一样使用参数化查询,你应该很好.

不要只是将某些字符串连接到SQL查询中. (并且不要在这样的方法中捕获并吞没所有异常.这使得在发生故障时很难调试.)

总结

以上是内存溢出为你收集整理的c# – 从MySQL中检索中等blob只返回13个字节全部内容,希望文章能够帮你解决c# – 从MySQL中检索中等blob只返回13个字节所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1218089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存