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

标签 c# arrays list datatable

我给了一个存储在数据库中的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/

相关文章:

java - 在 Java 中映射两个字符串数组

javascript - 如何迭代数组并将结果存储在单个对象或变量中?

使用列表的 C# 并行编程 - 读取线程安全吗?

c# - 为 Windows 8 商店应用程序启用 "Toast Notifications"

javascript - 增进理解 - 在循环内使用扩展运算符复制数组

java - Java 中的二维列表

python - 在 Python 中按不同级别排序

c# - 如何将字符串的换行符更改为空格字符

c# - 在 C# 中测试类库

c# - WPF - 如何使用 XAML 中的参数将 ViewModel 绑定(bind)到 View