如何在 C# 中从 DataRow 读取字节数组?
我有一个带有 DataTable
的 DataSet
,它通过 TableAdapter
正确填充单个 DataRow
。
我可以使用如下代码从 DataRow 中提取数据:
dataFileID = (int)this.dataFileDataRow["DataFileID"];
dataFileName = (string)this.dataFileDataRow["DataFileName"];
dataFileDate = (DateTime)this.dataFileDataRow["DataFileDate"];
我还有另一列,名为 varbinary(max)
类型的 DataFile。
当我尝试从与上面相同的 DataRow
中提取该列的数据时,我什么也得不到。
byte[] fileFromDatabase = (byte[])this.dataFileDataRow["DataFile"];
如果我在此位置放置一个断点,我可以查看 dataFileDataRow,查看 ItemArray 属性,并看到二进制数据位于 ItemArray 中的位置 5。
我尝试使用其索引直接访问 ItemArray,但字节数组未复制到 fileFromDatabase 变量。
我还注意到将 fileFromDatabase 添加到我的手表会产生此错误:
“名称‘fileFromDatabase’在当前上下文中不存在”
执行仍然与 fileFromDatabase 的定义在同一块中,所以我不明白它是如何脱离上下文的。
我将 Visual Studio 的配置设置为“发布”而不是“调试”。 这导致我在尝试检查 fileFromDatabase 时看不到我正在寻找的实时调试信息。 从发布切换到调试后,我现在可以在手表中看到变量,并可以验证上面的代码是否正常工作。
I have a DataSet
with a DataTable
that correctly fills a single DataRow
through a TableAdapter
.
I am able to pull data from the DataRow with code like this:
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)
.
When I try to pull that column's data from the same DataRow
as above I get nothing.
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.
I have tried access the ItemArray directly using its index but the byte array is not being copied to the fileFromDatabase variable.
I have also noticed that adding fileFromDatabase to my watch produces this error:
"The name 'fileFromDatabase' does not exist in the current context"
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码有效,请确保将调试器设置为针对“调试”而不是“发布”进行编译。
基思
The code above works, make sure you set your debugger to compile for Debug, NOT Release.
Keith