storing image to byte[] into Mysql using asp.net a

2019-08-30 00:57发布

问题:

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. !!