使用asp.net和c#将图像存储到byte []到Mysql中

问题描述:

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

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 

我正在使用以下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;
        }
    }

这是用于创建要插入到MySql

here is calling method for creating byte[] bt to insert into 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中时.结果像这样...

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

解决了很多难题之后...用?添加参数即可,而不是直接在插入查询中传递字节数组bt. :

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)并传递类似这样的值

cmd.Parameters.Add(?p1",bt); <-在此处添加参数p1的值

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

注意:如果使用MySql作为数据库结尾,那么我建议使用?代替@符号.

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