我给了一个存储在数据库中的byte[]
,
我从 sql 中获取我的 DataTable
中的字节数组
这是我的 DataTable
,System.byte[] 是我的图像,以字节为单位,存储在我的数据库中
现在我想把这个DataTable
转换成列表
这是我当前的代码
var answerList = (from rw in dt.AsEnumerable()
select new RegistrationAnswers()
{
responseID = rw["responseID"].ToString() == string.Empty ? 0 : Convert.ToInt32(rw["responseID"].ToString()),
responseRegID = rw["responseRegID"].ToString() == string.Empty ? 0 : Convert.ToInt32(rw["responseRegID"].ToString()),
responseAnswer = rw["responseAnswer"].ToString(),
_ResponseDocument = rw["responseDocument"], //here i want to validate if rw["responseDocument"] is null or not and if this is not null then assign the byte[] data to _ResponseDocument
formID=Convert.ToInt32(rw["formID"])
}).ToList();
当我更新我的代码时
//At top
byte[] tempByteArray = new byte[0];
_responseDocument = Convert.IsDBNull((byte[])rw["responseDocument"]) == false ? tempByteArray : (byte[])rw["responseDocument"],
出现以下错误
“无法将‘System.DBNull’类型的对象转换为‘System.Byte[]’类型。”
i want to validate if rw["responseDocument"] is null or not and if this is not null then assign the byte[] data to _ResponseDocument
最佳答案
尝试转换
rw["responseDocument"] == System.DBNull.Value ? new byte[0] : (byte[])rw["responseDocument"];
或
Convert.IsDBNull(rw["responseDocument"]) ? new byte[0] : (byte[])rw["responseDocument"];
关于c# - 从数据表获取字节数组到列表 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52872353/