使用asp.net和c#将图像存储到byte []到Mysql中 [英] storing image to byte[] into Mysql using asp.net and c#

查看:43
本文介绍了使用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. !!

这篇关于使用asp.net和c#将图像存储到byte []到Mysql中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆