gpt4 book ai didi

c# - 从数据表获取字节数组到列表 C#

转载 作者:太空宇宙 更新时间:2023-11-03 12:09:56 25 4
gpt4 key购买 nike

我给了一个存储在数据库中的byte[]

我从 sql 中获取我的 DataTable 中的字节数组

Image

这是我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com