简体   繁体   English

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

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

I have a DataSet with a DataTable that correctly fills a single DataRow through a TableAdapter . 我有一个带有DataTableDataSet ,它通过TableAdapter正确填充单个DataRow

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 DataRow as 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. 我将Visual Studio的配置设置为Release而不是Debug。 This was causing me to not see the real time debugging information I was looking for when trying to examine fileFromDatabase. 这导致我在尝试检查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. 从Release转换到Debug后,我现在可以在手表中看到变量,并且可以验证上面的代码是否正常工作。

The code above works, make sure you set your debugger to compile for Debug, NOT Release. 上面的代码有效,请确保将调试器设置为针对Debug,NOT Release进行编译。

Keith 基思

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM