简体   繁体   English

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

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

I am using Asp.net with C# and back-end MySql to keep Images as byte[] array with using BLOB datatype 我正在使用Asp.net with C#和后端MySql Asp.net with C#并使用BLOB datatypeImages as byte[] array保留Images as byte[] array

TABLE : ImageLog

ImgID                 int (auto increment)
ImageLogo             blob 

I am using following function to convert image to array... 我正在使用以下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 这是用于创建要插入MySql byte[] bt调用方法

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

inserting like... 像...插入

INSERT INTO IMAGELOG (ImageLogo) VALUES ('"+bt+"') ; 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 . 现在,Program可以完美运行,不会引起任何错误,但是当映像存储在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 提前致谢

Problem Solved After Lot's of Difficulties... Simply Add Parameters With ? 解决了很多难题之后...用?轻松添加参数 instead passing byte array bt directly within Insert Query...Something like this: 而是直接在Insert Query中传递字节数组bt ...

INSERT INTO IMAGELOG (ImageLogo) VALUES (?p1) and Pass values something like this INSERT INTO IMAGELOG (ImageLogo) VALUES (?p1)并传递类似以下的值

cmd.Parameters.Add("?p1", bt); cmd.Parameters.Add(“?p1”,bt); <-- Adding parameter p1 value here <-在此处添加参数p1值

Note: If you are using MySql as database end then i suggest to use ? 注意:如果您使用MySql作为数据库端,那么我建议使用? 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. 希望它能帮助您,亲爱的。 !! !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM