DataReader has rows and data, trying to read from it says “no data is present”

北城余情 提交于 2019-12-01 20:04:31

Just to clarify the answer, it was using the debugger since expanding the results view calls Read() and therefore it moves past the row. As Marc Gravell said in a comment: Debugger considered harmful

If you want to put the data into a file, start by loading a DataTable instead of using a DataReader. With the DataReader, as has been mentioned in the comments, you might want to iterate through the result set with a while loop

while (reader.Read())
{

}

The loop reads one row at a time and quits when all of the rows have been read. Once you move to the next row, the previous rows are no longer available unless you have put them into some other structure, like a list or DataTable.

But you can use a DataAdapater to fill a DataTable so there might not be a reason to use a DataReader. Then you can write to a file from the DataTable.

In any event, I don't see how this line could work.

FileName = Convert.ToString(reader["FileName"])

I can post additional code for either approach if you like.
HTH Harvey Sather

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!