如何从 C# 中的 DataRow 读取字节数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/285010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:38:37  来源:igfitidea点击:

How do you read a byte array from a DataRow in C#?

c#debuggingbytearraytableadapterdatarow

提问by Keith Sirmons

I have a DataSetwith a DataTablethat correctly fills a single DataRowthrough a TableAdapter.

我有一个DataSet通过 aDataTable正确填充单个的DataRowa TableAdapter

I am able to pull data from the DataRow with code like this:

我可以使用如下代码从 DataRow 中提取数据:

dataFileID = (int)this.dataFileDataRow["DataFileID"];
dataFileName = (string)this.dataFileDataRow["DataFileName"];
dataFileDate = (DateTime)this.dataFileDataRow["DataFileDate"];

I have another column called DataFile of type varbinary(max).

我有另一列名为 DataFile 的类型varbinary(max)

When I try to pull that column's data from the same DataRowas above I get nothing.

当我尝试从与DataRow上述相同的列中提取该列的数据时,我什么也没得到。

byte[] fileFromDatabase = (byte[])this.dataFileDataRow["DataFile"];

If I put a break point at this location, I can look into the dataFileDataRow, look into the ItemArray property and see that the binary data is sitting at position 5 in the ItemArray.

如果我在这个位置放置一个断点,我可以查看 dataFileDataRow,查看 ItemArray 属性并看到二进制数据位于 ItemArray 中的位置 5。

I have tried access the ItemArray directly using its index but the byte array is not being copied to the fileFromDatabase variable.

我尝试使用它的索引直接访问 ItemArray,但字节数组没有被复制到 fileFromDatabase 变量。

I have also noticed that adding fileFromDatabase to my watch produces this error:

我还注意到将 fileFromDatabase 添加到我的手表会产生此错误:

"The name 'fileFromDatabase' does not exist in the current context"

“当前上下文中不存在名称‘fileFromDatabase’”

The execution is still in the same block as the definition of fileFromDatabase so I do not understand how it would be out of context.

执行仍然与 fileFromDatabase 的定义在同一个块中,所以我不明白它是如何脱离上下文的。

I had Visual Studio's configuration set to Release instead of Debug. This was causing me to not see the real time debugging information I was looking for when trying to examine fileFromDatabase. After switching from Release to Debug, I am able to see the variable in the watch now and can verify that the code above is working correctly.

我将 Visual Studio 的配置设置为 Release 而不是 Debug。这导致我在尝试检查 fileFromDatabase 时看不到我正在寻找的实时调试信息。从 Release 切换到 Debug 后,我现在可以在 watch 中看到变量,并且可以验证上面的代码是否正常工作。

采纳答案by Keith Sirmons

The code above works, make sure you set your debugger to compile for Debug, NOT Release.

上面的代码有效,请确保将调试器设置为针对调试进行编译,而不是针对发布进行编译。

Keith

基思