日期:2014-05-16  浏览次数:20741 次

求助:如何将DBNull.Value赋值给 保存图片的byte[]数组?
本帖最后由 aqing721 于 2014-03-20 14:33:17 编辑

大家好!我在写一个考试系统,有些试题里面有图片,写了一个方法将图片存到byte[] imagebinnary 中,在方法中先判断该试题是否有图片,有则返回imagebinnary,没有就返回DBNull.Value. 但是按照如下写法 报:无法将类型System.DBNull隐式转换为byte[] 。求给位高手帮帮忙,该如何解决?感激不尽!


public static byte[] imagetobyte() //将图片转换为byte[]
        {
            if (Imagepath != "") //检查图片路径是否为空
            {
                FileStream filestream = new FileStream(Imagepath, FileMode.Open);
                BinaryReader binnaryreader = new BinaryReader(filestream);
                byte[] imagebinnary = binnaryreader.ReadBytes((int)filestream.Length);
                binnaryreader.Close();
                filestream.Close(); 
                return imagebinnary;
            }
            else
            {
                return DBNull.Value;  
            }
        }

------解决方案--------------------
直接返回Null就可以。

保存到数据库时,再判断是Null,的话使用DBNull.Value.
------解决方案--------------------
public static object imagetobyte()  用这个才行object