2016-05-16 64 views
0

不好意思再问一次这个问题.. 我只是简化了这个问题.. 这是我的问题:无法投射'System.DBNull'类型的对象来为MySQL图像键入'System.Byte []'

的错误观点是

Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte()) 

的代码是

Dim stream As New MemoryStream() 
    Dim command As New MySqlCommand("select Imageblob from employeedetail where EmployeeID = '" + TextBoxEmployeeID.Text + "'", cn)   
Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte()) 
stream.Write(image, 0, image.Length) 

感谢您的帮助!

回答

0

DBNull是您的查询将返回如果表中的字段为空值。你不能将一个DBNull对象转换为另一种类型。你可以做到以下几点,以避免错误

Dim image As Byte() 
If Not ISDBNull(DirectCast(command.ExecuteScalar(), Byte())) Then image = DirectCast(command.ExecuteScalar(), Byte()) 

这只是检查,如果一个字段的类型为DBNull其分配给image

+0

非常感谢您之前!这有助于我解决这个问题! :D再次感谢很多p3tch –

相关问题