【问题标题】:storing image to byte[] into Mysql using asp.net and c#使用 asp.net 和 c# 将图像存储到 byte[] 到 Mysql
【发布时间】:2012-09-30 01:53:04
【问题描述】:

我使用Asp.net with C# 和后端MySql 来保持Images as byte[] array 使用BLOB datatype

TABLE : ImageLog

ImgID                 int (auto increment)
ImageLogo             blob 

我正在关注function to convert image to array...

private byte[] ConvertImageToByteArray(FileUpload fuImgToByte)
    {
        byte[] ImageByteArray;
        try
        {
            MemoryStream ms = new MemoryStream(fuImgToByte.FileBytes);
            ImageByteArray = ms.ToArray();
            return ImageByteArray;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

这里是创建byte[] bt插入MySql的调用方法

Byte[] bt = null;
bt = ConvertImageToByteArray(FileUploader1); --> Passing File Uploader ControlID

插入...

INSERT INTO IMAGELOG (ImageLogo) VALUES ('"+bt+"');

现在,程序可以完美运行而不会导致任何错误,但是当图像存储到 MySql 中时,it stored like System.Byte[] not into byte[] array。结果是这样的……

ImgID      ImageLogo
________________________________
  1        System.Byte[]    13K ( Length )  < ----- > not storing byte[] in proper format
  2        System.Byte[]    13K ( Length )

请告诉我它的格式是否正确? ?或不 ??欢迎提出任何建议。 提前致谢

【问题讨论】:

    标签: mysql image bytearray blob store


    【解决方案1】:

    在遇到很多困难后解决了问题...只需使用? 添加参数,而不是直接在插入查询中传递字节数组bt...类似这样:

    INSERT INTO IMAGELOG (ImageLogo) VALUES (?p1) 和传递类似这样的值

    cmd.Parameters.Add("?p1", bt);

    注意:如果您使用MySql 作为数据库端,那么我建议使用? 而不是@ 符号。

    OUTPUT:

    ImgID      ImageLogo
    ________________________________
      1        Binary Image    73K ( Length ) < ----- > You can see the difference...
      2        Binary Image    69K ( Length )
    

    希望对大家有所帮助,Cears。 !!

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 2021-05-13
      • 1970-01-01
      • 2014-05-22
      • 2022-10-16
      • 2011-07-04
      • 2021-04-23
      • 2015-02-27
      • 1970-01-01
      相关资源
      最近更新 更多