storing image to byte[] into Mysql using asp.net and c#

痞子三分冷 提交于 2019-12-24 18:25:23

问题


I am using Asp.net with C# and back-end MySql to keep Images as byte[] array with using BLOB datatype

TABLE : ImageLog

ImgID                 int (auto increment)
ImageLogo             blob 

I am using following 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;
        }
    }

here is calling method for creating byte[] bt to insert into MySql

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

inserting like...

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

Now, Program runs perfectlly without causing any errors but when image stored into MySql, it stored like System.Byte[] not into byte[] array. Result Something like this...

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

Please tell me is it in proper format ? ? or not ?? Every suggestions are welcome. Thanks in advance


回答1:


Problem Solved After Lot's of Difficulties... Simply Add Parameters With ? instead passing byte array bt directly within Insert Query...Something like this:

INSERT INTO IMAGELOG (ImageLogo) VALUES (?p1) and Pass values something like this

cmd.Parameters.Add("?p1", bt); <-- Adding parameter p1 value here

Note: If you are using MySql as database end then i suggest to use ? instead @ symbol.

OUTPUT:

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

Hope It Helps You All, Chears. !!



来源:https://stackoverflow.com/questions/12805015/storing-image-to-byte-into-mysql-using-asp-net-and-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!