如何将字节数组分配到数据行列 [英] How to assign byte array into a datarow column

查看:88
本文介绍了如何将字节数组分配到数据行列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想为datarow列分配一个字节数组...当我分配如下后,该值显示为System.Byte [] 而不是实际的字节数组...



Hi,

I want to assign a byte array to datarow column...When i assigned like following the value is showing as "System.Byte[]" instead of actual byte array...

row1 = dsDetail.Tables["table"].NewRow();

dsDetail.Tables["table"].Columns["logo"].DataType=System.Type.GetType("System.Byte[]");
 byte[] P = ImageToByteArray(a);
 row1["logo"] = P;


//function used to get byte array

  public byte[] ImageToByteArray(string imagepath)
        {

            FileStream fs;

            fs = new FileStream(imagepath, FileMode.Open, FileAccess.Read);

            //a byte array to read the image

            byte[] picbyte = new byte[fs.Length];

            fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));

            fs.Close();

            return picbyte;
        }

推荐答案

是的,你可以。请遵循以下代码:



Yes, you can. please follow this code:

DataTable dt = new DataTable();
            dt.Columns.Add("imageByte", typeof(byte[]));

            byte[] imageByte = null;
            imageByte=ImageToByteArray(Server.MapPath(@"\images\yourImage.png"));

            DataRow drNew = dt.NewRow();
            drNew["imageByte"] = imageByte;

            dt.Rows.Add(drNew);





谢谢

Rashed ::孟加拉国



thanks
Rashed::Bangladesh


当您为列分配一个类并尝试显示它时,将调用ToString方法 - 如果未覆盖该方法,则执行默认的object.ToString方法。这打印了类的名称(因为它必须做某事)而不是内容(因为它可能不知道如何以人类可读的方式组织内容。



如果要显示数组内容,则需要创建自己的类,该类包含数组并自行覆盖ToString。
When you assign a class to a column, and try to display it, the ToString method is called - if this is not overridden, then the default object.ToString method is executed. This prints the name of the class (as it has to do something) rather than the contents (since it may not know how to organise the content in a human readable way.

If you want to display the array content, then you need to create your own class which holds an array and override ToString yourself.


这篇关于如何将字节数组分配到数据行列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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